164

How can I remove a button in Android, or make it invisible?

slhck
  • 36,575
  • 28
  • 148
  • 201
Troj
  • 11,781
  • 12
  • 40
  • 49

14 Answers14

385

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"/>
gregschlom
  • 4,825
  • 4
  • 28
  • 23
Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93
39

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"/>
Mick Ashton
  • 356
  • 4
  • 15
Thomas V J
  • 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
    Use "buttonCleanup.setVisibility(View.VISIBLE);" instead of the zero. – Vincent Jun 30 '15 at 10:00
17

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"/>
ghader
  • 369
  • 3
  • 9
14
button.setVisibility(View.GONE);
Korhan Ozturk
  • 11,148
  • 6
  • 36
  • 49
Ben Groot
  • 5,040
  • 3
  • 40
  • 47
13

This view is visible.

button.setVisibility(View.VISIBLE);

This view is invisible, and it doesn't take any space for layout purposes.

button.setVisibility(View.GONE); 

But if you just want to make it invisible:

button.setVisibility(View.INVISIBLE);
Neru-J
  • 1,623
  • 2
  • 23
  • 38
MSIslam
  • 4,587
  • 6
  • 25
  • 28
3

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

Mohammad
  • 6,024
  • 3
  • 22
  • 30
sajjad
  • 626
  • 1
  • 7
  • 25
3
button.setVisibility(button.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);

Makes it visible if invisible and invisible if visible

ORY
  • 393
  • 1
  • 4
  • 11
2

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.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
kundan kamal
  • 674
  • 7
  • 16
0

To completely remove a button from its parent layout:

((ViewGroup)button.getParent()).removeView(button);
Daniel
  • 1,599
  • 1
  • 16
  • 19
0
<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

Gowtham Subramaniam
  • 3,358
  • 2
  • 19
  • 31
0

In order to access elements from another class you can simply use

findViewById(R.id.**nameOfYourelementID**).setVisibility(View.GONE); 
FrankS101
  • 2,112
  • 6
  • 26
  • 40
0

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.

-1
Button btn=(Button)findViewById(R.id.btn);
btn.setVisibility(8);
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
-3

Try This Code :

button.setVisibility(View.INVISIBLE);
Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80
Ardi
  • 157
  • 1
  • 2
  • 9
    you 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