How to hide a button programmatically?

Android

Android Problem Overview


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 Solutions


Solution 1 - Android

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);
    }
});

Solution 2 - Android

Try the below code -

playButton.setVisibility(View.INVISIBLE);

or -

playButton.setVisibility(View.GONE);

show it again with -

playButton.setVisibility(View.VISIBLE);

Solution 3 - Android

In Kotlin

myButton.visibility = View.GONE

Solution 4 - Android

Hidde:

BUTTON.setVisibility(View.GONE);

Show:

BUTTON.setVisibility(View.VISIBLE);

Solution 5 - Android

Please used below

View.GONE and View.VISIBLE

Solution 6 - Android

public void OnClick(View.v)
Button b1 = (Button) findViewById(R.id.playButton);
b1.setVisiblity(View.INVISIBLE);

Solution 7 - Android

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");
    }
 }

Solution 8 - Android

        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);

Solution 9 - Android

Solution 10 - Android

Kotlin code is a lot simpler:

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

Solution 11 - Android

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

Solution 12 - Android

For "Xamarin Android":

FindViewById<Button>(Resource.Id.Button1).Visibility = ViewStates.Gone;

Solution 13 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRishiView Question on Stackoverflow
Solution 1 - AndroidSunil Kumar SahooView Answer on Stackoverflow
Solution 2 - AndroidBalaji.KView Answer on Stackoverflow
Solution 3 - AndroidjungledevView Answer on Stackoverflow
Solution 4 - AndroidAlex ZaraosView Answer on Stackoverflow
Solution 5 - AndroidNikhilView Answer on Stackoverflow
Solution 6 - AndroidfhiloView Answer on Stackoverflow
Solution 7 - AndroidFloView Answer on Stackoverflow
Solution 8 - Androidr3dm4nView Answer on Stackoverflow
Solution 9 - AndroidVladimir IvanovView Answer on Stackoverflow
Solution 10 - AndroidJustinView Answer on Stackoverflow
Solution 11 - AndroidBasilView Answer on Stackoverflow
Solution 12 - AndroidMatheus MirandaView Answer on Stackoverflow
Solution 13 - AndroidRehan KhanView Answer on Stackoverflow