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
Inherited properties

XML attributes

android:inflatedId

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

android:layout

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

Public constructors

ViewStub

Added in API level 1
ViewStub(context: Context!)

ViewStub

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

ViewStub

Added in API level 1
ViewStub(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int)

ViewStub

Added in API level 21
ViewStub(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int,
    defStyleRes: Int)

ViewStub

Added in API level 1
ViewStub(
    context: Context!,
    layoutResource: Int)

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.

Public methods

draw

Added in API level 1
fun draw(canvas: Canvas): Unit
Parameters
canvas Canvas: The Canvas to which the View is rendered. This value cannot be null.

getInflatedId

Added in API level 1
fun getInflatedId(): Int

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

Return
Int A positive integer used to identify the inflated view or NO_ID if the inflated view should keep its id.

getLayoutInflater

Added in API level 16
fun getLayoutInflater(): LayoutInflater!

Get current LayoutInflater used in inflate().

getLayoutResource

Added in API level 1
fun getLayoutResource(): Int

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

Return
Int The layout resource identifier used to inflate the new View.

inflate

Added in API level 1
fun inflate(): View!

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

Return
View! The inflated layout resource.

setInflatedId

Added in API level 1
fun setInflatedId(inflatedId: Int): Unit

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

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
fun setLayoutInflater(inflater: LayoutInflater!): Unit

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

setLayoutResource

Added in API level 1
fun setLayoutResource(layoutResource: Int): Unit

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.

Parameters
layoutResource Int: A valid layout resource identifier (different from 0.)

setOnInflateListener

Added in API level 1
fun setOnInflateListener(inflateListener: ViewStub.OnInflateListener!): Unit

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
fun setVisibility(visibility: Int): Unit

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
protected fun dispatchDraw(canvas: Canvas): Unit
Parameters
canvas Canvas: the canvas on which to draw the view This value cannot be null.

onMeasure

Added in API level 1
protected fun onMeasure(
    widthMeasureSpec: Int,
    heightMeasureSpec: Int
): Unit
Parameters
widthMeasureSpec Int: horizontal space requirements as imposed by the parent. The requirements are encoded with android.view.View.MeasureSpec.
heightMeasureSpec Int: vertical space requirements as imposed by the parent. The requirements are encoded with android.view.View.MeasureSpec.