ViewStub Android

To load “layout resource” at runtime, the ViewStub is used in Android. It is a zero-sized invisible View. We can not see anything on the layout pallet because it is a zero dimension View. The inflate() method is invoked to make the parent resource visible. The setVisibility(int) method is invoked to make ViewStub visible or invisible. To make the ViewStub visible, the View.VISIBLE constant is used. While to make the ViewStub invisible, the View.GONE constant is used.

Example of ViewStub:

In the below example, we will display and hide an ImageView (image) created in another layout file i.e., my_layout.xml file using the ViewStub View.

File: activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="MainActivity">
 
    <ViewStub
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/viewStub"
        android:layout_marginLeft="120dp"
        android:layout="@layout/my_layout"
        android:layout_alignParentTop="true" />
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show"
        android:id="@+id/show"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="65dp"
        android:layout_marginStart="65dp" />
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hide"
        android:id="@+id/hide"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/show"
        android:layout_toEndOf="@+id/show"
        android:layout_marginLeft="51dp"
        android:layout_marginStart="51dp" />
 
</RelativeLayout>

File: my_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:background="@drawable/php"
        />
 
</LinearLayout>

File: MainActivity.java:

package com.example.radioapp;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;
 
public class MainActivity extends AppCompatActivity {
    ViewStub viewStub;
    Button show,hide;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        show=(Button)findViewById(R.id.show);
        hide=(Button)findViewById(R.id.hide);
        viewStub=(ViewStub)findViewById(R.id.viewStub);
        viewStub.inflate();
 
        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewStub.setVisibility(View.VISIBLE);
            }
        });
        hide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewStub.setVisibility(View.GONE);
            }
        });
    }
}

Output 1:

Output 2:

Please Share