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?


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"/> <!-- idle state -->
</selector>

Vincent Ducastel's answer is correct, however it does not describe why the solution works.

When Android traverses the list of available items, it traverses the list from top to bottom, in each case evaluating whether the current state of the view matches the states defined for each item. It then selects the first item that matches the conditions and ignores the rest.

This is why you should always provide a default item at the bottom of the list and also provides a means of displaying complex selection conditions. For example if you wanted to have a special pressed state when the item is disabled, you would define the following items:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    ...
    <item android:state_enabled="false" android:state_pressed="false" android:drawable="@drawable/btn_grey_disabled"/>
    <item android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/btn_white_disabled_selected"/>
    ...
</selector>