Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do you make a button invisible after clicking?

setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Button button = (Button) v; button. setVisibility(View. INVISIBLE); } });

What is Android visibility?

A View's visibility status is of Integer type and can have one of three options: VISIBLE (0) - The View is visible to the user. INVISIBLE (4) - The View is invisible to the user, but still takes up space in the layout. GONE (8) - The View is invisible, and it does not take up space in the layout.


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

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

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

button.setVisibility(View.GONE);

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

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