Tuesday, March 16, 2021

Android ViewStub

 

Android ViewStub

ViewStub is a zero-sized invisible View which is used to load "layout resource" at runtime. ViewStub is a zero dimension View, so you will not see anything on the layout pallete.

To make parent resource visible, inflate() method is invoked. To make ViewStub visible or invisible, setVisibility(int)method is invoked. The View.VISIBLE constant is used for making ViewStub visible and View.GONE constant is used for invisible.

Example of ViewStub

Let's create an example of ViewStub View that displays and hides an ImageView (image) created in another layout (my_layout.xml) file.

File: activity.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context="com.example.test.viewstubexample.MainActivity">  
  11.   
  12.     <ViewStub  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:id="@+id/viewStub"  
  16.         android:layout_marginLeft="120dp"  
  17.         android:layout="@layout/my_layout"  
  18.         android:layout_alignParentTop="true" />  
  19.   
  20.     <Button  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:text="Show"  
  24.         android:id="@+id/show"  
  25.         android:layout_alignParentBottom="true"  
  26.         android:layout_alignParentLeft="true"  
  27.         android:layout_alignParentStart="true"  
  28.         android:layout_marginLeft="65dp"  
  29.         android:layout_marginStart="65dp" />  
  30.   
  31.     <Button  
  32.         android:layout_width="wrap_content"  
  33.         android:layout_height="wrap_content"  
  34.         android:text="Hide"  
  35.         android:id="@+id/hide"  
  36.         android:layout_alignParentBottom="true"  
  37.         android:layout_toRightOf="@+id/show"  
  38.         android:layout_toEndOf="@+id/show"  
  39.         android:layout_marginLeft="51dp"  
  40.         android:layout_marginStart="51dp" />  
  41.   
  42. </RelativeLayout>  

File: my_layout.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <ImageView  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:id="@+id/imageView"  
  10.         android:background="@drawable/image"  
  11.         />  
  12.   
  13. </LinearLayout>  

File: MainActivity.java

  1. package com.example.test.viewstubexample;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.ViewStub;  
  7. import android.widget.Button;  
  8.   
  9. public class MainActivity extends AppCompatActivity {  
  10.     ViewStub viewStub;  
  11.     Button show,hide;  
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.   
  17.         show=(Button)findViewById(R.id.show);  
  18.         hide=(Button)findViewById(R.id.hide);  
  19.         viewStub=(ViewStub)findViewById(R.id.viewStub);  
  20.         viewStub.inflate();  
  21.   
  22.         show.setOnClickListener(new View.OnClickListener() {  
  23.             @Override  
  24.             public void onClick(View v) {  
  25.                 viewStub.setVisibility(View.VISIBLE);  
  26.             }  
  27.         });  
  28.         hide.setOnClickListener(new View.OnClickListener() {  
  29.             @Override  
  30.             public void onClick(View v) {  
  31.                 viewStub.setVisibility(View.GONE);  
  32.             }  
  33.         });  
  34.     }  
  35. }  

Output

android ViewStub 1
android ViewStub 2 

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile