Set Visibility on Buttons in Android

Introduction

 
This article shows how to set visibility on buttons in Android.
 
Procedure
  • Start the Eclipse IDE.
  • Create a new project.
  • Create a MainActivity.java file.
  • Create an XML file with three buttons.
  • In the onClick function set the visibility of buttons using the setVisibility function.
For example
  1. public void onClick(View v) {    
  2.      b1.setVisibility(v.INVISIBLE);    
  3.      b2.setVisibility(v.VISIBLE);    
  4.  } 
The following is the code.
 
MainActivity.java
  1. package com.example.visibilityyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy;    
  2.      
  3.  import android.app.Activity;    
  4.  import android.os.Bundle;    
  5.  import android.view.Menu;    
  6.  import android.view.View;    
  7.  import android.view.View.OnClickListener;    
  8.  import android.widget.Button;    
  9.  import android.widget.Toast;    
  10.      
  11.  public class MainActivity extends Activity {    
  12.      Button b1,b2,b3;    
  13.      
  14.      @Override    
  15.      protected void onCreate(Bundle savedInstanceState) {    
  16.          super.onCreate(savedInstanceState);    
  17.          setContentView(R.layout.activity_main);    
  18.          b1=(Button)findViewById(R.id.button1);    
  19.          b2=(Button)findViewById(R.id.button2);    
  20.          b3=(Button)findViewById(R.id.button3);    
  21.          b1.setOnClickListener(new OnClickListener() {    
  22.              public void onClick(View v) {    
  23.                  // TODO Auto-generated method stub    
  24.                  b1.setVisibility(v.INVISIBLE);    
  25.                  b2.setVisibility(v.VISIBLE);    
  26.                  Toast.makeText(getApplicationContext(), "Button2 is visible",Toast.LENGTH_LONG).show();    
  27.              }    
  28.          });    
  29.          b2.setOnClickListener(new OnClickListener() {    
  30.              public void onClick(View v) {    
  31.                  // TODO Auto-generated method stub    
  32.                  b2.setVisibility(v.INVISIBLE);    
  33.                  b3.setVisibility(v.VISIBLE);    
  34.                  Toast.makeText(getApplicationContext(), "Button3 is visible ", Toast.LENGTH_LONG).show();    
  35.              }    
  36.          });    
  37.          b3.setOnClickListener(new OnClickListener() {    
  38.              public void onClick(View v) {    
  39.                  b3.setVisibility(v.INVISIBLE);    
  40.                  b1.setVisibility(v.VISIBLE);    
  41.                  Toast.makeText(getApplicationContext(), "Button1 is visible", Toast.LENGTH_LONG).show();    
  42.                  // TODO Auto-generated method stub    
  43.              }    
  44.          });    
  45.      }    
  46.      @Override    
  47.      public boolean onCreateOptionsMenu(Menu menu) {    
  48.          // Inflate the menu; this adds items to the action bar if it is present.    
  49.          getMenuInflater().inflate(R.menu.main, menu);    
  50.          return true;    
  51.      }    
  52.  }   
    activity_main.xml
    1. <RelativeLayout  
    2.     xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     xmlns:tools="http://schemas.android.com/tools"  
    4.   
    5.    android:layout_width="match_parent"  
    6.    android:layout_height="match_parent"  
    7.    android:paddingBottom="@dimen/activity_vertical_margin"  
    8.    android:paddingLeft="@dimen/activity_horizontal_margin"  
    9.    android:paddingRight="@dimen/activity_horizontal_margin"  
    10.    android:paddingTop="@dimen/activity_vertical_margin"  
    11.    tools:context=".MainActivity" >  
    12.       
    13. <TextView  
    14.       android:id="@+id/textView1"  
    15.       android:layout_width="wrap_content"  
    16.       android:layout_height="wrap_content"  
    17.       android:text="Abhijeet's game of visibility" />  
    18.         
    19. <Button  
    20.       android:id="@+id/button1"  
    21.       android:layout_width="wrap_content"  
    22.       android:layout_height="wrap_content"  
    23.       android:layout_alignTop="@+id/textView1"  
    24.       android:layout_marginLeft="14dp"  
    25.       android:visibility="visible"  
    26.       android:layout_toRightOf="@+id/textView1"  
    27.       android:text="Button1" />  
    28.         
    29. <Button  
    30.       android:id="@+id/button2"  
    31.       android:layout_width="wrap_content"  
    32.       android:layout_height="wrap_content"  
    33.       android:layout_alignRight="@+id/button1"  
    34.       android:layout_below="@+id/button1"  
    35.       android:visibility="gone"  
    36.       android:layout_marginRight="40dp"  
    37.       android:text="Button2" />  
    38.         
    39. <Button  
    40.       android:id="@+id/button3"  
    41.       android:layout_width="wrap_content"  
    42.       android:layout_height="wrap_content"  
    43.       android:layout_alignRight="@+id/button2"  
    44.       android:layout_below="@+id/button2"  
    45.       android:visibility="gone"  
    46.       android:layout_marginRight="15dp"  
    47.       android:layout_marginTop="26dp"  
    48.       android:text="Button3" />  
    49.         
    50. </RelativeLayout> 
       
    Output
     
    When clicking on Button1:
     
     
    When clicking on Button2:
      
     
    When clicking on Button3:
     


    Similar Articles