443

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

Braiam
  • 1
  • 11
  • 47
  • 78
Dijo David
  • 6,175
  • 11
  • 35
  • 46

13 Answers13

918

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.

hichris123
  • 10,145
  • 15
  • 56
  • 70
Varun
  • 33,833
  • 4
  • 49
  • 42
58

Yes it can be disabled in XML just using:

<Button
android:enabled="false"
/>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Mulaffer
  • 651
  • 5
  • 6
  • 7
    This is the real answer for xml. All that nonsense of messing with clickable has nothing to do with how the button is drawn to make it look disabled. – SMBiggs Feb 03 '17 at 17:41
  • 3
    I think people thought you can't do this because AS/IDEA code completion doesn't suggest `android:enabled` when you start to type it. (Even to this day.) – Kevin Krumwiede Aug 05 '18 at 18:36
  • 4
    I tried it is not working, only from code `setEnabled` is working – FindOutIslamNow Aug 14 '18 at 05:24
52

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.

hichris123
  • 10,145
  • 15
  • 56
  • 70
Gwen
  • 537
  • 4
  • 2
45

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);
hichris123
  • 10,145
  • 15
  • 56
  • 70
Deepak Sharma
  • 4,999
  • 5
  • 51
  • 61
31

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" />
Paolo Rovelli
  • 9,396
  • 2
  • 58
  • 37
10

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));
Arda Yigithan Orhan
  • 1,062
  • 11
  • 18
10

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
Sackurise
  • 2,804
  • 1
  • 18
  • 18
7

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
All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111
4

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)  
ceph3us
  • 7,326
  • 3
  • 36
  • 43
  • 2
    Seems like any time the else expression would get called, the code is already bugged. You were expecting a Button but something else was passed. You *should* throw an exception when `v` isn't a `Button`. – Jeroen Jul 08 '15 at 00:50
  • Not quite what I meant, but an improvement nonetheless. – Jeroen Jul 09 '15 at 23:41
  • @JeroenBollen eelaborate please :) ok i see only one thing not consistent it's exception message about "wrong view passed to method".Besides it's just an attempt to show how it should look like. – ceph3us Jul 09 '15 at 23:50
  • I meant that when the function itself receives null, it's already a bug and it should throw an exception. – Jeroen Jul 10 '15 at 00:03
4

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

4

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

myButton.setEnabled(false);
vibhu norby
  • 156
  • 4
3

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

        }
    });
}
sampathsris
  • 21,564
  • 12
  • 71
  • 98
rajeesh
  • 937
  • 10
  • 11
1

Just use setEnabled method in Java.

myButton.setEnabled(false); 

And in Kotlin

myButton.enabled = false
4b0
  • 21,981
  • 30
  • 95
  • 142
Vijay Antil
  • 51
  • 1
  • 3