ViewStub


public final class ViewStub
extends View

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


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 = 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

android:inflatedId Overrides the id of the inflated View with this value. 
android:layout Supply an identifier for the layout resource to inflate when the ViewStub becomes visible or when forced to do so. 

Inherited XML attributes

Inherited constants

Inherited fields

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 defStyleAttr)
ViewStub(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

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 View.VISIBLE or View.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.

Inherited methods

XML attributes

android:inflatedId

Overrides the id of the inflated View with this value.

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

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.

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

Related methods:

Public constructors

ViewStub

Added in API level 1
public ViewStub (Context context)

Parameters
context Context

ViewStub

Added in API level 1
public ViewStub (Context context, 
                int layoutResource)

Creates a new ViewStub with the specified layout resource.

Parameters
context Context: The application's environment.

layoutResource int: The reference to a layout resource that will be inflated.

ViewStub

Added in API level 1
public ViewStub (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

ViewStub

Added in API level 1
public ViewStub (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

ViewStub

Added in API level 21
public ViewStub (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

draw

Added in API level 1
public void draw (Canvas canvas)

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.
If you override this method you must call through to the superclass implementation.

Parameters
canvas Canvas: The Canvas to which the View is rendered. This value cannot be null.

getInflatedId

Added in API level 1
public int getInflatedId ()

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

Related XML Attributes:

Returns
int A positive integer used to identify the inflated view or View.NO_ID if the inflated view should keep its id.

See also:

getLayoutInflater

Added in API level 16
public LayoutInflater getLayoutInflater ()

Get current LayoutInflater used in inflate().

Returns
LayoutInflater

getLayoutResource

Added in API level 1
public 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.

Related XML Attributes:

Returns
int The layout resource identifier used to inflate the new View.

inflate

Added in API level 1
public View inflate ()

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

Returns
View The inflated layout resource.

setInflatedId

Added in API level 1
public void setInflatedId (int inflatedId)

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

Related XML Attributes:

Parameters
inflatedId int: A positive integer used to identify the inflated view or View.NO_ID if the inflated view should keep its id.

See also:

setLayoutInflater

Added in API level 16
public void setLayoutInflater (LayoutInflater inflater)

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

Parameters
inflater LayoutInflater

setLayoutResource

Added in API level 1
public void setLayoutResource (int layoutResource)

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 int: A valid layout resource identifier (different from 0.)

setOnInflateListener

Added in API level 1
public void setOnInflateListener (ViewStub.OnInflateListener inflateListener)

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

Parameters
inflateListener ViewStub.OnInflateListener: The OnInflateListener to notify of successful inflation.

setVisibility

Added in API level 1
public void setVisibility (int visibility)

When visibility is set to View.VISIBLE or View.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 int: One of View.VISIBLE, View.INVISIBLE, or View.GONE.

See also:

Protected methods

dispatchDraw

Added in API level 1
protected void dispatchDraw (Canvas canvas)

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 Canvas: the canvas on which to draw the view This value cannot be null.

onMeasure

Added in API level 1
protected void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

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 overridden 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 int: horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

heightMeasureSpec int: vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.