Hide Show button in android programmatically

How to set button visibility visible or Gone in android dynamically.

Android button can be hide and show at application run time using setVisibility() method. With the use of this method app developer can easily hide and again show the button. In this tutorial we are creating three button and from two button we are showing and hiding our third Same button. So here is the complete step by step tutorial for Hide Show button in android programmatically.

android-project-download-code-button

How to Hide Show button in android programmatically.

Code for MainActivity.java file.

 package com.hideshowbutton_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

 Button button1,button2,button3;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 button1 = (Button)findViewById(R.id.button1);
 button2 = (Button)findViewById(R.id.button2);
 button3 = (Button)findViewById(R.id.button3);
 
 button1.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 //Hiding sample button.
 button3.setVisibility(View.GONE);
 
 }
 });
 
 button2.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 //Showing sample button.
 button3.setVisibility(View.VISIBLE);
 
 }
 });
 }
}

Code for activity_main.xml layout file.

 <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"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.hideshowbutton_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Click Here Hide Sample button" />

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button1"
 android:layout_centerHorizontal="true"
 android:text="Click Here Show Sample button" />

 <Button
 android:id="@+id/button3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/button1"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="46dp"
 android:text="Sample Button" />

</RelativeLayout>

Screenshot:

Hide Show button in android programmatically

hide-button

Click here to download Hide Show button in android programmatically project file with source code.