How to Remove a Button or Make It Invisible in Android

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

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

how to use visible and invisible for a button in android

DONT USE -

donebutton.setVisibility(4);

Instead use the static constants for this:

donebutton.setVisibility(View.VISIBLE);

What exactly means

done.setVisibility(0);

Isn't is supposed to be

donebutton.setVisibility(View.GONE);

How to remove Button from layout?

onClick set button.setVisibility(View.GONE);

How to hide a button programmatically?

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

Android studio - invisible button

You need to create selector under drawable folder and set color or image over there for different sates of the button.

I have created one sample application for you and am pasting code snippet:

Code for button_selector.xml that contains selector code for button which you will need to put under drawable folder:


<item android:drawable="@drawable/no_box_selector" android:state_pressed="true"></item>
<item android:drawable="@drawable/no_box"></item>


You will need to put no_box_selector and no_box pngs in drawable-hdpi or any drawable folders

Code for activity_main.xml that will be your main xml that contains only one button as of now:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relRingtone"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical" >

<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/button_selector"
android:padding="5dp"
android:text="Click Me"
android:textColor="#FFF"
android:textSize="20sp"
android:textStyle="bold" />

</RelativeLayout>

Try implementing these things and let me know if you need any further assisstance.....

All the best!!!

Reference : Disable pressed background color of button

Hide the text on a button in android?

I have a suggestion if you want to setText to a button but don't want to show it.

Just set the text in the xml

android:text="TEXT"

then make font to 0

android:textSize="0sp"

text exist but can't be seen.

How to remove button shadow (android)

Another alternative is to add

style="?android:attr/borderlessButtonStyle"

to your Button xml as documented here
http://developer.android.com/guide/topics/ui/controls/button.html

An example would be

<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"
style="?android:attr/borderlessButtonStyle" />

How do I make a button invisible just after click?


button.setOnClickListener(new OnClickListener() {

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

This makes it go invisible but still take up space in the layout, switching the last row for:

                button.setVisibility(View.GONE);

would make it "fold" and it will not only be invisible but won't take up space in the layuout either.

Android studio Hide and Show buttons from a condition

From your comments, are you saying you need to check:

if(Cars.payment) {
btnDone.setVisibility(View.GONE);
btnPaid.setVisibility(View.VISIBLE);
} else {
btnPaid.setVisibility(View.GONE);
btnDone.setVisibility(View.VISIBLE);
}

removing a Button from a LinearLayout

you need to just change invisible to gone like below code:

yourbutton.setVisibility(View.GONE);

if you are using gone it's hide button with remove occupy space in layout!

after you need show button using visible it's automatically re-occupy spaces for a button in layout like below sample code:

yourbutton.setVisibility(View.VISIBLE);

if you are invisible it's hides only button and it's does not remove occupy space button in layout like below code:

yourbutton.setVisibility(View.INVISIBLE);


Related Topics



Leave a reply



Submit