Most visited

Recently visited

Added in API level 1

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

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

From class android.view.View

Inherited constants

From class android.view.View

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

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

ViewStub

Added in API level 1
ViewStub (Context context)

Parameters
context Context

ViewStub

Added in API level 1
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
ViewStub (Context context, 
                AttributeSet attrs)

Parameters
context Context
attrs AttributeSet

ViewStub

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

Parameters
context Context
attrs AttributeSet
defStyleAttr int

ViewStub

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

Parameters
canvas Canvas: The Canvas to which the View is rendered.

getInflatedId

Added in API level 1
int getInflatedId ()

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
int A positive integer used to identify the inflated view or NO_ID if the inflated view should keep its id.

See also:

getLayoutInflater

Added in API level 16
LayoutInflater getLayoutInflater ()

Get current LayoutInflater used in inflate().

Returns
LayoutInflater

getLayoutResource

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

See also:

inflate

Added in API level 1
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
void setInflatedId (int inflatedId)

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 int: A positive integer used to identify the inflated view or NO_ID if the inflated view should keep its id.

See also:

setLayoutInflater

Added in API level 16
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
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.)

See also:

setOnInflateListener

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

See also:

setVisibility

Added in API level 1
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. After that calls to this function are passed through to the inflated view.

Parameters
visibility int: One of VISIBLE, INVISIBLE, or GONE.

See also:

Protected methods

dispatchDraw

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

onMeasure

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

Hooray!