Android ViewStub Tutorial With Example

Hello Readers! In this post, we are going to learn about how to use android viewStub widget in any android application. We will also go through different attributes of viewStub widget that can be used to customise it.

Output

Tutorialwing Android ViewStub Output Android ViewStub Tutorial With Example

Tutorialwing Android ViewStub Output

Getting Started

ViewStub widget can be defined as below –

ViewStub is an invisible, zero-sized view that can be used to lazily inflate layout resource at run time. To inflate a view, we call either inflate() or setVisible(true) method. ViewStub then replaces itself in it’s parent with view or views. The inflated View is added to the ViewStub’s parent with the ViewStub’s layout parameters.

Attributes of Android ViewStub Widget

Some of the popular attributes of android ViewStub widget are –

Sr. XML Attributes Description
1 android:inflatedId It is used to set id of the inflated view by viewStub
2 android:layout It is used to provide identifier of the layout resource to inflate when viewStub is visible or forced to do so(by calling inflate() method)

Some of the popular attributes of android viewStub inherited from View are –

Sr. XML Attributes Description
1 android:alpha Sets alpha of the view
2 android:background Sets background of the view
3 android:focusable Sets whether view can take focus or not
4 android:id Sets unique identifier of the view
5 android:padding Set padding of view
6 android:visibility Sets whether this view is visible or not



Example of Android ViewStub Widget

At first, we will create android application. Then, we will use viewStub widget in this application.

1. Creating New Project

Follow steps below to create new project. Please ignore the steps if you have already created a new application.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as ViewStub. Then, click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify xml and java file to use viewStub widget in the application.

2. Modify Values folder

Open res/values/strings.xml file. Then, add below code into it.

<resources>
	<string name="app_name">ViewStub</string>
	<string name="hide">HIDE</string>
	<string name="show">SHOW</string>
	<string name="sub_view">This view is inflated by ViewStub by calling either inflate() or setVisibility(true) method</string>
</resources>

Now, we will create view that replaces viewStub when viewStub.inflated() or viewStub.setVisibility(true) is called.

3. Create xml file For Sub View

Create an xml file , named sub_item.xml, in res/layout folder. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:background="@android:color/holo_blue_light"
	android:gravity="center"
	android:padding="10dp">

	<TextView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:gravity="center"
		android:text="@string/sub_view"
		android:textColor="@android:color/black"
		android:textSize="18sp"/>

</LinearLayout>

Here, we have defined view that will replace viewStub when viewStub.inflate() is called.

4. Use ViewStub Widget in xml file

Open res/layout/activity_main.xml file. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:layout_margin="10dp"
	android:gravity="center_horizontal"
	android:orientation="vertical">

	<Button
		android:id="@+id/button"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="@string/hide"/>

	<ViewStub
		android:id="@+id/viewStub"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout="@layout/sub_item"/>

</LinearLayout>

In activity_main.xml file, we have defined viewStub widget. android:layout=”” is used to provide layout resource id to the viewStub that will replace viewStub. Now, we will access viewStub in MainActivity.java file to perform some operations on it.

5. Access ViewStub Widget in java file

Open src/main/java/com.tutorialwing.viewstub/MainActivity.java file. Then, add below code into it.

package com.tutorialwing.viewstub;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

	boolean isViewInflated = true; // Default state that represents viewStub has been replaced by sub view

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final ViewStub viewStub = findViewById(R.id.viewStub);
		if (viewStub != null) {
			viewStub.setVisibility(View.VISIBLE); // or you can use viewStub.inflate()

			final Button button = findViewById(R.id.button);
			if (button != null) {
				button.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						isViewInflated = !isViewInflated;
						viewStub.setVisibility(isViewInflated ? View.VISIBLE : View.GONE);
						button.setText(isViewInflated ? getString(R.string.hide) : getString(R.string.show));
					}
				});
			}
		}
	}
}

In MainActivity.java file, we have accessed viewStub widget. Then, we have called setVisibility(true) method. After that, we have set click listener that show hide view inflated by viewStub.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.

AndroidManifest.xml

Code inside src/main/AndroidManifest.xml file is as below –

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.viewstub"
		  xmlns:android="http://schemas.android.com/apk/res/android">

	<application
		android:allowBackup="true"
		android:icon="@mipmap/ic_launcher"
		android:label="@string/app_name"
		android:roundIcon="@mipmap/ic_launcher_round"
		android:supportsRtl="true"
		android:theme="@style/AppTheme">
		<activity android:name=".MainActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>
			</intent-filter>
		</activity>
	</application>

</manifest>

When we run the program, we will get output as shown above.

That’s end of tutorial on Android ViewStub widget.

Leave a Reply