Troj
Troj

Reputation: 11911

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

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

Upvotes: 164

Views: 328707

Answers (14)

Lakshya Chopra
Lakshya Chopra

Reputation: 69

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.

Upvotes: 0

Martynas Stanys
Martynas Stanys

Reputation: 11

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

findViewById(R.id.**nameOfYourelementID**).setVisibility(View.GONE); 

Upvotes: 0

kundan kamal
kundan kamal

Reputation: 672

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.

Upvotes: 2

ORY
ORY

Reputation: 413

button.setVisibility(button.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);

Makes it visible if invisible and invisible if visible

Upvotes: 3

Thomas V J
Thomas V J

Reputation: 401

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"/>

Upvotes: 40

Gowtham Subramaniam
Gowtham Subramaniam

Reputation: 3478

<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

Upvotes: 0

Daniel
Daniel

Reputation: 1699

To completely remove a button from its parent layout:

((ViewGroup)button.getParent()).removeView(button);

Upvotes: 0

Ardi
Ardi

Reputation: 157

Try This Code :

button.setVisibility(View.INVISIBLE);

Upvotes: -3

Konstantin Burov
Konstantin Burov

Reputation: 69228

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"/>

Upvotes: 385

sajjad
sajjad

Reputation: 666

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

Upvotes: 3

user2922138
user2922138

Reputation: 19

Button btn=(Button)findViewById(R.id.btn);
btn.setVisibility(8);

Upvotes: -1

MSIslam
MSIslam

Reputation: 4727

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

Upvotes: 13

Ben Groot
Ben Groot

Reputation: 5050

button.setVisibility(View.GONE);

Upvotes: 14

ghader
ghader

Reputation: 379

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"/>

Upvotes: 17

Related Questions