You are on page 1of 5

How to hide a button programmatically?

Ask Question

I have a RelativeLayout which


contains two buttons. Which are
overlapped on each other.

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">

<Button android:text="Play"
android:id="@+id/play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom = "true">
</Button>

<Button android:text="Stop "


android:id="@+id/stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom = "true">
</Button>

</RelativeLayout>

I want to programmatically show only


one button at a time when its click
event is called.

I tried it with :

playButton.setVisibility(1);

but it does not worked. Following is


an example what I am trying to do.

playButton = (Button) findViewById(R.id.play);


playButton.setVisibility(1);
playButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//when play is clicked show stop button and hide play button

}
});

android

Join Stack Overflow to learn, share knowledge, and build your career.
editedour
This site uses cookies to deliver Aug 24 '16 atand
services 3:27to show you relevant ads and job listings. By using our site, you
Jorge B.
acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Your use of
319 2 10 28
Stack Overflow’s Products and Services, including the StackSign Up Network, is subject to these policies and terms.
Overflow
asked May 30 '11 at 7:26
Rishi
1,339 7 29 53

10 Answers

¿No encuentras la ✕
respuesta? Pregunta en
Stack Overflow en
español.

You can use the following code:

playButton = (Button) findViewById(R.i


playButton.setVisibility(View.VISIBLE)
playButton.setOnClickListener(new OnCl
@Override
public void onClick(View v) {
//when play is clicked show st
playButton.setVisibility(View.
stopButton.setVisibility(View.
}
});

edited Oct 14 '17 at 21:53


Angie Loo
3 2

answered May 30 '11 at 7:42


Sunil Kumar Sahoo
36.1k 45 157 232

2 Thanks sunil :) can you please tell the


difference between View.VISIBLe and
1 ( is it just enum ) ? –
Vamsi Krishna B Jan 2 '12 at 13:34

1 Why setVisibility to 1? That's not any


of the constant values. – pqsk Aug 16
'13 at 17:46

3 View.GONE makes the item not take


up any layout space. View.INVISIBLE
reserves space for the item. This
changes the layout of view when you
toggle visibility. – gb96 Jun 13 '15 at
10:19

Try the below code - to learn, share knowledge, and build your career.
This site uses cookies to deliver our services and to show you relevant ads and job listings. By using our site, you
acknowledge that you have read and understand our
playButton.setVisibility(View.INVISIBL , , and our . Your use of
Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.
or -

playButton.setVisibility(View.GONE);

show it again with -

playButton.setVisibility(View.VISIBLE)

edited Aug 13 '13 at 19:43


naveends
3 3

answered May 30 '11 at 7:38


Balaji.K
7,609 5 23 37

5 +1 for using View.GONE instead of 1


– Thorsten Niehues Oct 3 '13 at 17:21

Please used below

View.GONE and View.VISIBLE

answered May 30 '11 at 7:36


Nikhil
14.1k 19 56 78

Hidde:

BUTTON.setVisibility(View.GONE);

Show:

BUTTON.setVisibility(View.VISIBLE);

answered May 27 '15 at 23:01


Alex Zaraos
3,689 1 17 19

public void OnClick(View.v)


Button b1 = (Button) findViewById(R.id
b1.setVisiblity(View.INVISIBLE);

edited Mar 11 '16 at 6:57


Hussein El Feky
4,317 2 32 47

answered May 30 '12 at 14:17


to learn, share knowledge, and build your career.
This site uses cookies to deliver ourfhilo
services and to show you relevant ads and job listings. By using our site, you
51 1 1
acknowledge that you have read and understand our , , and our . Your use of
Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.
I would suggest you only use one
button an change the text and the
behavior on the button on demand.
That's easier and cleaner than
handling two buttons which are
overlapping.

@Override
public void onClick(View v) {
String curText = ((TextView)v).get

if(curText.equals("Play")){
((TextView)v).setText("Stop");
}

if(curText.equals("Stop")){
((TextView)v).setText("Play");
}
}

edited May 30 '11 at 9:38

answered May 30 '11 at 7:37


Flo
22k 13 76 115

i like your idea its actually what i do in


iphone toggling single button to do
multiple things.But i am new to
android , can you please point me to
an example on how to do this.. –
Rishi May 30 '11 at 9:07

Try View.INVISIBLE .

answered May 30 '11 at 7:29


Vladimir Ivanov
36.9k 15 68 96

Please try this: playButton = (Button)


findViewById(R.id.play);
playButton.setVisibility(View.INVISIB
LE); I think this will do it.

answered May 30 '11 at 7:38


Basil
2,155 2 12 25

Button button = (Button) findV


//set to visible to learn, share knowledge, and build your career.
This site uses cookies to deliver our services and to show you relevant ads and job listings. By using our site, you
button.setVisibility(View.VISI
//set to invisble
acknowledge that you have read and understand our , , and our . Your use of
button.setVisibility(View.INVI
Stack Overflow’s Products //or
and Services, including the Stack Overflow Network, is subject to these policies and terms.
button.setVisibility(View.GONE
edited Sep 20 '17 at 18:04

answered Sep 19 '16 at 5:12


r3dm4n
574 2 10 23

For "Xamarin Android":

FindViewById<Button>(Resource.Id.Butto

answered Mar 25 at 3:06


Matheus Miranda
422 1 6 16

to learn, share knowledge, and build your career.


This site uses cookies to deliver our services and to show you relevant ads and job listings. By using our site, you
acknowledge that you have read and understand our , , and our . Your use of
Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.

You might also like