How can I remove a button in Android, or make it invisible?
14 Answers
Set button visibility to GONE (button will be completely "removed" -- the buttons space will be available for another widgets) or INVISIBLE (button will became "transparent" -- its space will not be available for another widgets):
View b = findViewById(R.id.button);
b.setVisibility(View.GONE);
or in xml:
<Button ... android:visibility="gone"/>

- 4,825
- 4
- 28
- 23

- 68,980
- 16
- 115
- 93
-
when you set it to gone does it still count as a child? if i did something like child count or get child at index what ever? – Lpc_dark Dec 27 '12 at 20:41
-
1I think yes, it does still count as a child. – Konstantin Burov Dec 28 '12 at 02:07
-
12And to show it like this.. b.setVisibility(View.VISIBLE); – Zar E Ahmer May 19 '14 at 10:56
-
Any way to enable/make it visible through javascript executor or anything? – Ashok kumar Ganesan Aug 11 '20 at 07:00
First make the button invisible in xml file.Then set button visible in java code if needed.
Button resetButton=(Button)findViewById(R.id.my_button_del);
resetButton.setVisibility(View.VISIBLE); //To set visible
Xml:
<Button
android:text="Delete"
android:id="@+id/my_button_del"
android:layout_width="72dp"
android:layout_height="40dp"
android:visibility="invisible"/>

- 356
- 4
- 15

- 391
- 3
- 2
-
I want make the button invisible, but that should be accesible in activity, much like Super secret button. It should not show up in activity, but the button should work – Samrat Mazumdar Jun 30 '12 at 15:56
-
1
To remove button in java code:
Button btn=(Button)findViewById(R.id.btn);
btn.setVisibility(View.GONE);
To transparent Button in java code:
Button btn=(Button)findViewById(R.id.btn);
btn.setVisibility(View.INVISIBLE);
To remove button in Xml file:
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
To transparent button in Xml file:
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>

- 369
- 3
- 9
use setVisibility in button or imageViwe or .....
To remove button in java code:
Button btn=(Button)findViewById(R.id.btn);
btn.setVisibility(Button.GONE);
To transparent Button in java code
Button btn=(Button)findViewById(R.id.btn);
btn.setVisibility(Button.INVISIBLE);
You should make you button xml code like below:
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
hidden:
visibility: gone
show:
visibility: invisible
visibility: visible
button.setVisibility(button.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
Makes it visible if invisible and invisible if visible

- 393
- 1
- 4
- 11
IF you want to make invisible button, then use this:
<Button ... android:visibility="gone"/>
View.INVISIBLE:
Button will become transparent. But it taking space.
View.GONE
Button will be completely remove from the layout and we can add other widget in the place of removed button.

- 55,572
- 24
- 139
- 212

- 674
- 7
- 16
To completely remove a button from its parent layout:
((ViewGroup)button.getParent()).removeView(button);

- 1,599
- 1
- 16
- 19
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/activity_register_header"
android:minHeight="50dp"
android:orientation="vertical"
android:visibility="gone" />
Try This Code
Visibility works fine in this code

- 3,358
- 2
- 19
- 31
In order to access elements from another class you can simply use
findViewById(R.id.**nameOfYourelementID**).setVisibility(View.GONE);

- 2,112
- 6
- 26
- 40

- 11
- 3
If you want to make your button invisible such that it doesn't take any space in the layout, then add this in your Java code:
Button button = (Button)findViewById(R.id.button);
button.setVisibility(View.GONE);
Or in XML: android:visibility="gone"
If you want to make your button invisible such that it still takes up space in your layout then replace "View.GONE" with "View.INVISIBLE" in your java code or replace "gone" with "invisible" in your xml code.

- 49
- 1
- 3
Button btn=(Button)findViewById(R.id.btn);
btn.setVisibility(8);

- 44,500
- 61
- 101
- 156

- 19
- 2
Try This Code :
button.setVisibility(View.INVISIBLE);

- 8,954
- 10
- 58
- 80

- 157
- 1
- 2
-
9you really shouldn't use hard-coded values in this method -- just use the Constants -- View.GONE, View.VISIBLE or View.INVISIBLE – bkurzius Feb 18 '13 at 01:01