174

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

    }
});
Jorge B.
  • 1,144
  • 2
  • 17
  • 37
Rishi
  • 3,499
  • 8
  • 35
  • 54

13 Answers13

346

You can use the following code:

playButton = (Button) findViewById(R.id.play);
playButton.setVisibility(View.VISIBLE);
playButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        //when play is clicked show stop button and hide play button
        playButton.setVisibility(View.GONE);
        stopButton.setVisibility(View.VISIBLE);
    }
});
Angie Loo
  • 3
  • 2
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • 2
    Thanks sunil :) can you please tell the difference between View.VISIBLe and 1 ( is it just enum ) ? – Vamsi Krishna B Jan 02 '12 at 13:34
  • 2
    Why setVisibility to 1? That's not any of the constant values. – pqsk Aug 16 '13 at 17:46
  • 7
    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
88

Try the below code -

playButton.setVisibility(View.INVISIBLE);

or -

playButton.setVisibility(View.GONE);

show it again with -

playButton.setVisibility(View.VISIBLE);
naveends
  • 3
  • 3
Balaji.K
  • 8,745
  • 5
  • 30
  • 39
11

In Kotlin

myButton.visibility = View.GONE

jungledev
  • 4,195
  • 1
  • 37
  • 52
9

Hidde:

BUTTON.setVisibility(View.GONE);

Show:

BUTTON.setVisibility(View.VISIBLE);
Alex Zaraos
  • 6,443
  • 2
  • 26
  • 21
8

Please used below

View.GONE and View.VISIBLE
Nikhil
  • 16,194
  • 20
  • 64
  • 81
6
public void OnClick(View.v)
Button b1 = (Button) findViewById(R.id.playButton);
b1.setVisiblity(View.INVISIBLE);
Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
fhilo
  • 61
  • 1
  • 1
4
        Button button = (Button) findViewById(R.id.myButton);
        //set to visible
        button.setVisibility(View.VISIBLE);
        //set to invisble      
        button.setVisibility(View.INVISIBLE);
       //or
        button.setVisibility(View.GONE);
r3dm4n
  • 1,175
  • 2
  • 18
  • 33
4

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).getText();                 

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

    if(curText.equals("Stop")){
        ((TextView)v).setText("Play");
    }
 }
Flo
  • 27,355
  • 15
  • 87
  • 125
  • 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 09:07
4

Java

first connect button or any view you want to make it invisible or visible

btn = findViewById(R.id.btn);

then first make it visible as below

btn.setVisibility(View.VISIBLE);

I am using onClickListener to show or hide so when the button is clicked one button will be hidden and second will be shown

btn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        //when button is clicked this button will be hidden
        btn.setVisibility(View.GONE);
        //  OR 
        btn.setVisibility(View.INVISIBLE);

        // here the second button will be show as below
        btn11.setVisibility(View.VISIBLE);
    }
});

Kotlin

//For Making Button hide
btn.visibility = View.INVISIBLE

//For showing Button
btn.visibility = View.VISIBLE
Rehan Khan
  • 1,031
  • 13
  • 10
3

Kotlin code is a lot simpler:

if(clearButton.isVisible) {
    clearButton.visibility = View.INVISIBLE
} else {
    clearButton.visibility = View.VISIBLE
}

Fun fact:

Setting isVisible property to true sets the visibility to View.VISIBLE, false to View.GONE.

view.isVisible = true is equal to view.visibility = View.VISIBLE
view.isVisible = false is equal to view.visibility = View.GONE
Sudheesh R
  • 1,767
  • 3
  • 23
  • 43
Justin
  • 945
  • 12
  • 26
3

Try View.INVISIBLE.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
3

Please try this: playButton = (Button) findViewById(R.id.play); playButton.setVisibility(View.INVISIBLE); I think this will do it.

Basil
  • 2,163
  • 2
  • 16
  • 25
1

For "Xamarin Android":

FindViewById<Button>(Resource.Id.Button1).Visibility = ViewStates.Gone;
Matheus Miranda
  • 1,755
  • 2
  • 21
  • 36