State list drawable and disabled state

AndroidAndroid Layout

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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>

Solution 2 - Android

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>

Solution 3 - Android

Added example of Active, Default and Disable State

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!--Disable-->
    <item android:drawable="@drawable/ic_button_disable"
        android:state_enabled="false" 
        android:state_pressed="false" />

    <item android:drawable="@drawable/ic_button_disable_touch" 
        android:state_enabled="false" 
        android:state_pressed="true" />


    <!--Default-->
    <item android:drawable="@drawable/ic_button_default"  
        android:state_pressed="false"    
        android:state_selected="false" />

    <item android:drawable="@drawable/ic_button_default_touch" 
        android:state_pressed="true" 
        android:state_selected="false"  />


    <!--Active-->
    <item android:drawable="@drawable/ic_button_active" 
        android:state_enabled="true" 
        android:state_selected="true" 
        android:state_pressed="false" />

    <item android:drawable="@drawable/ic_button_active_touch" 
        android:state_enabled="true" 
        android:state_pressed="true" 
        android:state_selected="true" />
</selector>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser468311View Question on Stackoverflow
Solution 1 - AndroidVincent DucastelView Answer on Stackoverflow
Solution 2 - AndroidTheITView Answer on Stackoverflow
Solution 3 - AndroidHitesh SahuView Answer on Stackoverflow