Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 489
Dijo David Avatar asked Dec 08 '10 06:12

Dijo David


People also ask

How do I make my Android button disabled?

You could Also make it appear as disabled by setting the alpha (making it semi-transparent). This is especially useful if your button background is an image, and you don't want to create states for it. button. setAlpha(.

Can buttons be disabled?

A disabled button is unusable and un-clickable. The disabled attribute can be set to keep a user from clicking on the button until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the button clickable again.

Can you disable the Back button in Android?

Please navigate to Android->Advanced Restrictions->Display Settings. Uncheck the 'Hide navigation bar' option. This will disable the on-screen buttons: back, home and recent apps.


2 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.

like image 70
Varun Avatar answered Sep 30 '22 22:09

Varun


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.

like image 29
Gwen Avatar answered Sep 30 '22 21:09

Gwen