Dijo David
Dijo David

Reputation: 6195

How to disable an Android button?

I have created a layout that contains two buttons, Next and Previous. In between the buttons I'm generating some dynamic views. So when I first launch the application I want to disable the "Previous" button since there wont be any previous views. I also want to disable the "Next" button when there are not more views to display. Is there anyway to disable the buttons?

screen shot of sample layout

Upvotes: 444

Views: 657178

Answers (13)

vibhu norby
vibhu norby

Reputation: 156

You can disable a button from your xml but that won't be dynamic. Best way to disable button dynamically is.

myButton.setEnabled(false);

Upvotes: 4

Vijay Antil
Vijay Antil

Reputation: 61

Just use setEnabled method in Java.

myButton.setEnabled(false); 

And in Kotlin

myButton.enabled = false

Upvotes: 1

Adeeksha Sathsara
Adeeksha Sathsara

Reputation: 183

If you need to disable button add this line of code.

Button button = findViewById(R.id.button)
button.setEnabled(false);

And enable button , just add this line

 button.setEnabled(true);

Happy coding :D

Upvotes: 4

All Іѕ Vаиітy
All Іѕ Vаиітy

Reputation: 26372

With Kotlin you can do,

// to disable clicks
myButton.isClickable = false 

// to disable button
myButton.isEnabled = false

// to enable clicks
myButton.isClickable = true 

// to enable button
myButton.isEnabled = true

Upvotes: 7

Mulaffer
Mulaffer

Reputation: 661

Yes it can be disabled in XML just using:

<Button
android:enabled="false"
/>

Upvotes: 59

Sackurise
Sackurise

Reputation: 2814

In Kotlin, if you refer the Button View with id then, enable/disable button as like

layout.xml

<Button
   android:id="@+id/btn_start"
   android:layout_width="100dp"
   android:layout_height="50dp"
   android:text="@string/start"
   android:layout_alignParentBottom="true"/>

activity.kt

  btn_start.isEnabled = true   //to enable button
  btn_start.isEnabled = false  //to disable button

Upvotes: 10

ceph3us
ceph3us

Reputation: 7474

WRONG WAY IN LISTENER TO USE VARIABLE INSTEAD OF PARAMETER!!!

btnSend.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        btnSend.setClickable(false);

    }
});

RIGHT WAY:

btnSend.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        /** check given view  by assertion or cast as u wish */
        if(v instance of Button) {

            /** cast */
            Button button = (Button) v;

            /** we can perform some check up */
            if(button.getId() == EXPECTED_ID) {

                /** disable view */
                button.setEnabled(false)            
                button.setClickable(false); 
            }

        } else {

             /** you can for example find desired view by root view  */
             Button bt = (Button) v.getRootView().findViewById(R.id.btId);

             /*check for button */
             if(bt!=null) {

                 /** disable button view */
                 ...
             } else {
                 /** according to @jeroen-bollen remark
                   * we made assumption that we expected a view
                   * of type button here in other any case  
                   */
                  throw new IllegalArgumentException("Wrong argument: " +
                         "View passed to method is not a Button type!");
             }
          }
       }
    });

EDIT: In reply to @jeroen-bollen

 View.OnClickListener 

is Interface definition for a callback to be invoked when a view is clicked.

with method definition

void onClick(View v);

when the view is clicked the View class object makes callback to method onClick() sending as parameter itself, so null view parameter should not occur if it does it's an Assertion Error it could happen for example when View object class was destroyed in meanwhile (for example collected by GC) or method was tampered due to hack

little about instanceof & null

JLS / 15.20.2. Type Comparison Operator instanceof

At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException.

Otherwise the result is false.


three words from the Author

IF U ASK WHY ?

MOSTLY TO AVOID NullPointerException

Little more code will save your time on later bug tracking in your code & reduces the occurrence of abnomalies.

consider following example:

View.OnClickListener listener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        btnSend.setClickable(false);

    }
});

btnSend.setOnClickListener(listener)
btnCancel.setOnClickListener(listener)  

Upvotes: 4

rajeesh
rajeesh

Reputation: 941

first in xml make the button as android:clickable="false"

<Button
        android:id="@+id/btn_send"
        android:clickable="false"/>

then in your code, inside oncreate() method set the button property as

btn.setClickable(true);

then inside the button click change the code into

btn.setClickable(false);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    btnSend = (Button) findViewById(R.id.btn_send);
    btnSend.setClickable(true);
    btnSend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            btnSend.setClickable(false);

        }
    });
}

Upvotes: 3

Gwen
Gwen

Reputation: 537

You can't enable it or disable it in your XML (since your layout is set at runtime), but you can set if it's clickable at the launch of the activity with android:clickable.

Upvotes: 52

Deepak Sharma
Deepak Sharma

Reputation: 5063

You just write a single line of code in your activity

Button btn = (Button) findViewById(R.id.button1);
btn.setEnabled(false);

When you want to enable the same button just write

Button btn = (Button) findViewById(R.id.button1);
btn.setEnabled(true);

Upvotes: 47

Varun
Varun

Reputation: 33983

Did you try this?

myButton.setEnabled(false); 

Update: Thanks to Gwen. Almost forgot that android:clickable can be set in your XML layout to determine whether a button can be clickable or not.

Upvotes: 922

Paolo Rovelli
Paolo Rovelli

Reputation: 9684

In Java, once you have the reference of the button:

Button button = (Button) findviewById(R.id.button);

To enable/disable the button, you can use either:

button.setEnabled(false);
button.setEnabled(true);

Or:

button.setClickable(false);
button.setClickable(true);

Since you want to disable the button from the beginning, you can use button.setEnabled(false); in the onCreate method. Otherwise, from XML, you can directly use:

android:clickable = "false"

So:

<Button
        android:id="@+id/button"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/button_text"
        android:clickable = "false" />

Upvotes: 31

Arda Yigithan Orhan
Arda Yigithan Orhan

Reputation: 1052

In my case,

myButton.setEnabled(false);
myButton.setEnabled(true);

is working fine and it is enabling and disabling the button as it should. But once the button state becomes disabled, it never goes back to the enabled state again, although it's clickable. I tried invalidating and refreshing the drawable state, but no luck.

myButton.invalidate();
myButton.refreshDrawableState();

If you or anyone having a similar issue, what works for me is setting the background drawable again. Works on any API Level.

myButton.setEnabled(true);
myButton.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.myButtonDrawable));

Upvotes: 10

Related Questions