What is viewstub

A ViewStub is an invisible, zero-sized View that is used to inflate layout resources at runtime. When a ViewStub is made visible, or when inflate() is invoked, the layout resource is inflated.

In my program you click on hide button it will hide the image and the text of button will change into show and again you click on button the image will display and the text of button will change into hide.


ViewStub:.

In this section i am going to show the viewstub with practical example.

First open the android studio and create a project and give the name of your project like ViewStubDemo.

The screenshots of the program is as follows:

Smiley face Smiley face

Drag and drop the one ViewStub and one Button from pallet onto activity_main.xml file . Open the activity_main.xml file and code look like this


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">

    <ViewStub
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/viewStub"

        android:layout="@layout/my_layout"
        android:layout_alignParentTop="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hide"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="120dp"
         />



</RelativeLayout>
       

You need to do one thing. create a resource file(abc.layout) for this you right click on layout a dialog box will display click on new->Layout resource file->a dialog box will display where you need to write the file name it is abc. The image show dialog box.

Smiley face

drag and drop one ImageView into abc.layout file.The code is look like this.

<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/screen"
        />



</LinearLayout>
 

Open the MainActivity.java file and code look like this.


package com.example.niitpc.myapplication;

import android.app.DatePickerDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import java.sql.Time;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    ViewStub viewStub;
    Button button;
  int i=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button=(Button)findViewById(R.id.button);
        viewStub=(ViewStub)findViewById(R.id.viewStub);
        viewStub.inflate();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(i==1) {
                    viewStub.setVisibility(View.VISIBLE);
                    button.setText("Hide");

                    i=i+1;
                }
                else
                {
                    button.setText("Show");
                    viewStub.setVisibility(View.GONE);
                    i=1;
                }
            }
        });


    }
}


       

Explanation:

ViewStub viewStub:This statement declare the viewstub object of ViewStub.

Button show,hide:This statement declare the button object of Button.

int i=1:This statement declare the int variable i and store the value 1.

button=(Button)findViewById(R.id.button):This statement initialized the button object.

viewStub=(ViewStub)findViewById(R.id.viewStub):This statement initialized the viewstub object.

viewStub.inflate():This statement inflate the viewstub object.

button.setOnClickListener(new View.OnClickListener()Here we create the listener for button.

viewStub.inflate():This statement inflate the viewstub object.

if(i==1):This statement check the conditon if the value of i==1 then the body of the if is execute.

viewStub.setVisibility(View.VISIBLE):This statement visible the viewstub.

button.setText("Hide"):This statement set the text of button as hide.

i=i+1:This statement add the 1 value in i and assign into i .

else:This statement will execute when the if is false .

button.setText("Hide"):This statement set the text of button as hide.

button.setText("Show"):This statement set the text of button as show.

viewStub.setVisibility(View.GONE):This statement make the viewstub invisible.

i=1:This statement assign the 1 into variable i.


ViewStub

What is viewstub for video Click here

Smiley face                     Smiley face