Introduction
In this blog, you will learn how to develop ViewStub and the Image Hide and Show process, using ViewStub in Android Studio
Requirements
If you want to develop the Image Hide and show functionality, follow the below steps.
Step 1
Open Android Studio and go to File >> New >> New Project.

Step 2
Now, write the application name and click the "Next" button.

Here, we can choose the target Android devices and the minimum SDK version.

Step 3
Now, at the next screen, select Empty Activity and click the "Next" button.

Here, write the Activity name and click the "Finish" button.

Step 4
Now, in the New project, go to design page (activity_main.xml). If you want to add some options, just follow the drag and drop method to add ViewStub, imageView, button, Textview.

Here, you will see the design page (Graphical User Interface).

Now, look for XML code in activity_main.xml code.
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="xyz.rvconstructions.www.viewstubapp.MainActivity">
- <Button android:id="@+id/showButton" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="50dp" android:background="#ff7300" android:text="Show" android:textColor="#fff" android:textSize="18sp" android:textStyle="bold" />
- <Button android:id="@+id/hideButton" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="10dp" android:background="#ffaa00" android:text="Hide" android:textColor="#fff" android:textSize="18sp" android:textStyle="bold" />
- <ViewStub android:id="@+id/simpleViewStub" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:inflatedId="@+id/inflatedview" android:layout="@layout/custom_stub" /> </RelativeLayout>
Now, you will add the new XML page. Here, go to Layout and click open the new window. Go to New >> Layout resource file

Here, write the XML page name like custom_stub.xml and click the "OK" button.


Here, add the image in drawable folder.

Step 6
Now, build the design in new XML page.

Here, you will see the design page (Graphical User Interface).

Now, add the following XML code in custom_stub.xml code.
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
- <ImageView android:layout_width="wrap_content" android:layout_height="200dp" android:layout_gravity="center" android:src="@drawable/image" />
- <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="BABY" android:textColor="#000" /> </LinearLayout>









Join the conversation! Your thoughts help the community grow.