Attributes of ViewStub

Attributes

Description

id To uniquely identify a ViewStub
inflated To set the id of the inflated View. Via programmatically we can set it by using the setInflatedId() method.
layout

To supply an identifier for the layout resource to inflate when the ViewStub becomes visible or when forced to do so.

Via programmatically can be set by using the setLayoutResource(int) method.

Example

A sample GIF is given below to get an idea about what we are going to do in this article.

ViewStub in Android with Example

In Android, ViewStub is a zero-sized invisible View and very flexible in that we can lazily inflate layout resources at runtime. It is a dumb and lightweight view and it has zero dimension. Depending upon the requirement, whenever in need we can inflate at runtime. This is a classic example to handle when there are complicated views to be handled. When the ViewStub is inflated, it replaces itself in its parent view with the inflated layout resource. Its visibility will exist in the view hierarchy only when setVisibility(int) or inflate() is invoked. The inflated view is added to ViewStub’s parent using the layout parameter. Whenever there are item details, undo messages, or progress indicators, we can think of ViewStub. It reduces memory usage and speeds up rendering by loading the views only when they are needed. One can think that it looks similar to including a tag but it is not. Include tag includes contents in the XML file which is present under the layout folder. So we can have header, footer, left, and right bar XMLs separately but still they are included in the main XML by using include tags. On the other hand, ViewStub is not included but it directly gets loaded when set as setVisibility(int)(for true option) or inflate() is invoked.

Similar Reads

Important Methods

First, let us get the reference of ViewStub...

Attributes of ViewStub

Attributes Description id To uniquely identify a ViewStub inflated To set the id of the inflated View. Via programmatically we can set it by using the setInflatedId() method. layout To supply an identifier for the layout resource to inflate when the ViewStub becomes visible or when forced to do so. Via programmatically can be set by using the setLayoutResource(int) method....

Step by Step Implementation

Step 1: Create a New Project in Android Studio...

Contact Us