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?
13 Answers
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.

- 10,145
- 15
- 56
- 70

- 33,833
- 4
- 49
- 42
-
73the equivalent is android:enabled in xml. – Namratha Feb 26 '13 at 09:35
-
1And where did you find that in the documentation? Can you link it here? – Varun Feb 27 '13 at 04:13
-
5@Namratha it is working just for "Button" widgets, not for imageView as a button – mehmet Apr 16 '14 at 08:47
-
Xamarin equivalent: myButton.Enabled = false; – Cameron Forward Sep 27 '17 at 12:01
Yes it can be disabled in XML just using:
<Button
android:enabled="false"
/>

- 8,084
- 8
- 48
- 62

- 651
- 5
- 6
-
7This 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
-
3I 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
-
4I tried it is not working, only from code `setEnabled` is working – FindOutIslamNow Aug 14 '18 at 05:24
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
.

- 10,145
- 15
- 56
- 70

- 537
- 4
- 2
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);

- 10,145
- 15
- 56
- 70

- 4,999
- 5
- 51
- 61
-
28That is **2** lines. This is one. `((Button)findViewById(R.id.mybutton)).setEnabled(false);` – Matt Clark Nov 10 '13 at 19:50
-
44
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" />

- 9,396
- 2
- 58
- 37
-
-
1@Poldie: Actually, if you read the two answers you will understand they are pretty similar but not equal. – Paolo Rovelli Feb 05 '14 at 14:45
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));

- 1,062
- 11
- 18
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

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

- 24,861
- 16
- 87
- 111
-
1// to enable button myButton.isEnabled = false <----- should be true – NullPointer Nov 16 '20 at 10:35
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)

- 7,326
- 3
- 36
- 43
-
2Seems 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
-
-
@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
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

- 163
- 1
- 9
You can disable a button from your xml but that won't be dynamic. Best way to disable button dynamically is.
myButton.setEnabled(false);

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

- 21,564
- 12
- 71
- 98

- 937
- 10
- 11
Just use setEnabled method in Java.
myButton.setEnabled(false);
And in Kotlin
myButton.enabled = false

- 21,981
- 30
- 95
- 142

- 51
- 1
- 3