Android ViewStub

A ViewStub is a zero-sized invisible View which is used to load "layout resource" at runtime. ViewStub is a zero dimension View, so you will not see anything on the layout pallete.

To make parent resource visible, inflate() method is invoked. To make ViewStub visible or invisible, setVisibility(int) method is invoked. The View.VISIBLE constant is used for making ViewStub visible and View.GONE constant is used for invisible.

Example of ViewStub

Let's create an example of ViewStub View that displays and hides an ImageView (image) created in another layout (my_layout.xml) file.

File: activity.xml

File: my_layout.xml

File: MainActivity.java

Output

android ViewStub 1
android ViewStub 2
Next TopicAndroid TabLayout




Contact US

Email:[email protected]

ViewStub
10/30