Igor Filippov Igor Filippov - 5 months ago 69
Android Question

State list drawable and disabled state

I have a button and I want it to have different background when I set:

android:enabled="false"


Here's resource file for background:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:drawable="@drawable/bttn_orange_normal" /> <!-- pressed -->
<item android:state_pressed="true"
android:drawable="@drawable/bttn_orange_selected" /> <!-- focused -->
<item android:state_enabled="false" android:drawable="@drawable/bttn_grey_disabled"/>
<item android:state_enabled="true" android:drawable="@drawable/bttn_orange_normal"/>
</selector>


But button still has normal background when it is disabled.
What am I missing?

Answer

Put this line : <item android:state_enabled="false" android:drawable="@drawable/bttn_grey_disabled"/> as first item (it must be first item, otherwise it will not work) of the 'selector' tag.

Final :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable="@drawable/bttn_grey_disabled"/>
    <item android:state_pressed="false"
      android:drawable="@drawable/bttn_orange_normal" /> <!-- pressed -->
    <item android:state_pressed="true"
      android:drawable="@drawable/bttn_orange_selected" /> <!-- focused -->
    <item android:state_enabled="true" android:drawable="@drawable/bttn_orange_normal"/>