Added in API level 1

ViewStub


class ViewStub : View
kotlin.Any
   ↳ 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
abstract

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
Public constructors
ViewStub(context: Context!)

ViewStub(context: Context!, attrs: AttributeSet!)

ViewStub(context: Context!, attrs: AttributeSet!, defStyleAttr: Int)

ViewStub(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)

ViewStub(context: Context!, layoutResource: Int)

Creates a new ViewStub with the specified layout resource.

Public methods
Unit
draw(canvas: Canvas)

Int

Returns the id taken by the inflated view.

LayoutInflater!

Get current LayoutInflater used in inflate().

Int

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

View!

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

Unit
setInflatedId(inflatedId: Int)

Defines the id taken by the inflated view.

Unit

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

Unit
setLayoutResource(layoutResource: Int)

Specifies the layout resource to inflate when this StubbedView becomes visible or invisible or when inflate() is invoked.

Unit

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

Unit
setVisibility(visibility: Int)

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
Unit

Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

Inherited functions