Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable button with custom background android

When I have an android button and set it as disabled then the button seems as "transparent".

I have a button with custom background for the pressed and focused state. I want to disable it from code. When I try button.setEnabled(false); I disable the button but it keeps the same custom background. Any idea how to disable my custom button and changes the background as transparent too?

like image 631
Nikitas Avatar asked Apr 26 '11 12:04

Nikitas


People also ask

How do I disable Android buttons?

Navigate to Android > Restrictions > Basic and click on Configure. Under Allow Device Functionality, you'll have the options to disable Home/Power button. Home Button-Uncheck this option to restrict users from using the Home Button. Power Off-Uncheck this option to restrict users from turning their devices off.


2 Answers

you should create you custom selector like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
    android:state_enabled="false"
        android:drawable="@drawable/btn_red" />
    <item 
        android:state_pressed="true" 
        android:state_enabled="true"
        android:drawable="@drawable/btn_orange" />
    <item 
        android:state_focused="true" 
        android:state_enabled="true"
        android:drawable="@drawable/btn_orange" />
    <item 
        android:state_enabled="true"
        android:drawable="@drawable/btn_black" />
</selector>
like image 71
pClass Avatar answered Nov 01 '22 18:11

pClass


Maybe you could use android:state_enabled and set that to appropriate transparent drawable in your selector for this button?

like image 16
Kocus Avatar answered Nov 01 '22 19:11

Kocus