to top
Android APIs
public final class

ViewStub

extends View
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewStub

Class Overview

A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime. When a ViewStub is made visible, or when inflate() is invoked, the layout resource is inflated. The ViewStub then replaces itself in its parent with the inflated View or Views. Therefore, the ViewStub exists in the view hierarchy until setVisibility(int) or inflate() is invoked. The inflated View is added to the ViewStub's parent with the ViewStub's layout parameters. Similarly, you can define/override the inflate View's id by using the ViewStub's inflatedId property. For instance:

     <ViewStub android:id="@+id/stub"
               android:inflatedId="@+id/subTree"
               android:layout="@layout/mySubTree"
               android:layout_width="120dip"
               android:layout_height="40dip" />
 
The ViewStub thus defined can be found using the id "stub." After inflation of the layout resource "mySubTree," the ViewStub is removed from its parent. The View created by inflating the layout resource "mySubTree" can be found using the id "subTree," specified by the inflatedId property. The inflated View is finally assigned a width of 120dip and a height of 40dip. The preferred way to perform the inflation of the layout resource is the following:
     ViewStub stub = (ViewStub) findViewById(R.id.stub);
     View inflated = stub.inflate();
 
When inflate() is invoked, the ViewStub is replaced by the inflated View and the inflated View is returned. This lets applications get a reference to the inflated View without executing an extra findViewById().

Summary

Nested Classes
interface ViewStub.OnInflateListener Listener used to receive a notification after a ViewStub has successfully inflated its layout resource. 
XML Attributes
Attribute Name Related Method Description
android:inflatedId setInflatedId(int) Overrides the id of the inflated View with this value. 
android:layout setLayoutResource(int) Supply an identifier for the layout resource to inflate when the ViewStub becomes visible or when forced to do so. 
[Expand]
Inherited XML Attributes
From class android.view.View
[Expand]
Inherited Constants
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
ViewStub(Context context)
ViewStub(Context context, int layoutResource)
Creates a new ViewStub with the specified layout resource.
ViewStub(Context context, AttributeSet attrs)
ViewStub(Context context, AttributeSet attrs, int defStyle)
Public Methods
void draw(Canvas canvas)
Manually render this view (and all of its children) to the given Canvas.
int getInflatedId()
Returns the id taken by the inflated view.
LayoutInflater getLayoutInflater()
Get current LayoutInflater used in inflate().
int getLayoutResource()
Returns the layout resource that will be used by setVisibility(int) or inflate() to replace this StubbedView in its parent by another view.
View inflate()
Inflates the layout resource identified by getLayoutResource() and replaces this StubbedView in its parent by the inflated layout resource.
void setInflatedId(int inflatedId)
Defines the id taken by the inflated view.
void setLayoutInflater(LayoutInflater inflater)
Set LayoutInflater to use in inflate(), or null to use the default.
void setLayoutResource(int layoutResource)
Specifies the layout resource to inflate when this StubbedView becomes visible or invisible or when inflate() is invoked.
void setOnInflateListener(ViewStub.OnInflateListener inflateListener)
Specifies the inflate listener to be notified after this ViewStub successfully inflated its layout resource.
void setVisibility(int visibility)
When visibility is set to VISIBLE or INVISIBLE, inflate() is invoked and this StubbedView is replaced in its parent by the inflated layout resource.
Protected Methods
void dispatchDraw(Canvas canvas)
Called by draw to draw the child views.
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

[Expand]
Inherited Methods
From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

XML Attributes

android:inflatedId

Overrides the id of the inflated View with this value.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This corresponds to the global attribute resource symbol inflatedId.

Related Methods

android:layout

Supply an identifier for the layout resource to inflate when the ViewStub becomes visible or when forced to do so. The layout resource must be a valid reference to a layout.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This corresponds to the global attribute resource symbol layout.

Related Methods

Public Constructors

public ViewStub (Context context)

Added in API level 1

public ViewStub (Context context, int layoutResource)

Added in API level 1

Creates a new ViewStub with the specified layout resource.

Parameters
context The application's environment.
layoutResource The reference to a layout resource that will be inflated.

public ViewStub (Context context, AttributeSet attrs)

Added in API level 1

public ViewStub (Context context, AttributeSet attrs, int defStyle)

Added in API level 1

Public Methods

public void draw (Canvas canvas)

Added in API level 1

Manually render this view (and all of its children) to the given Canvas. The view must have already done a full layout before this function is called. When implementing a view, implement onDraw(android.graphics.Canvas) instead of overriding this method. If you do need to override this method, call the superclass version.

Parameters
canvas The Canvas to which the View is rendered.

public int getInflatedId ()

Added in API level 1

Returns the id taken by the inflated view. If the inflated id is NO_ID, the inflated view keeps its original id.

Related XML Attributes
Returns
  • A positive integer used to identify the inflated view or NO_ID if the inflated view should keep its id.

public LayoutInflater getLayoutInflater ()

Added in API level 16

Get current LayoutInflater used in inflate().

public int getLayoutResource ()

Added in API level 1

Returns the layout resource that will be used by setVisibility(int) or inflate() to replace this StubbedView in its parent by another view.

Related XML Attributes
Returns
  • The layout resource identifier used to inflate the new View.

public View inflate ()

Added in API level 1

Inflates the layout resource identified by getLayoutResource() and replaces this StubbedView in its parent by the inflated layout resource.

Returns
  • The inflated layout resource.

public void setInflatedId (int inflatedId)

Added in API level 1

Defines the id taken by the inflated view. If the inflated id is NO_ID, the inflated view keeps its original id.

Related XML Attributes
Parameters
inflatedId A positive integer used to identify the inflated view or NO_ID if the inflated view should keep its id.
See Also

public void setLayoutInflater (LayoutInflater inflater)

Added in API level 16

Set LayoutInflater to use in inflate(), or null to use the default.

public void setLayoutResource (int layoutResource)

Added in API level 1

Specifies the layout resource to inflate when this StubbedView becomes visible or invisible or when inflate() is invoked. The View created by inflating the layout resource is used to replace this StubbedView in its parent.

Related XML Attributes
Parameters
layoutResource A valid layout resource identifier (different from 0.)

public void setOnInflateListener (ViewStub.OnInflateListener inflateListener)

Added in API level 1

Specifies the inflate listener to be notified after this ViewStub successfully inflated its layout resource.

Parameters
inflateListener The OnInflateListener to notify of successful inflation.

public void setVisibility (int visibility)

Added in API level 1

When visibility is set to VISIBLE or INVISIBLE, inflate() is invoked and this StubbedView is replaced in its parent by the inflated layout resource. After that calls to this function are passed through to the inflated view.

Parameters
visibility One of VISIBLE, INVISIBLE, or GONE.
See Also

Protected Methods

protected void dispatchDraw (Canvas canvas)

Added in API level 1

Called by draw to draw the child views. This may be overridden by derived classes to gain control just before its children are drawn (but after its own view has been drawn).

Parameters
canvas the canvas on which to draw the view

protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

Added in API level 1

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overriden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters
widthMeasureSpec horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.