Statelist drawable android:state_enabled not working in xml
Asked Answered
I

2

16

I'm trying to add to my existing statelist drawable, a disabled state and it just doesn't work.

originally, I had this code:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/store_item_background_selected" android:state_selected="true"/>
<item android:drawable="@drawable/store_item_background"/>
</selector>

and it worked perfectly for selected and not selected.

now I wanted to add the android:state_enabled="false" like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/store_item_background_selected" android:state_selected="true"/>
<item android:drawable="@drawable/store_item_background" android:state_enabled="true"/>
<item android:drawable="@drawable/store_item_background_disabled"/>
</selector>

and it never switches to the disabled image.

any ideas?

EDIT

I added setEnabled(false) to the constructor of the view I'm setting this statelist drwable and now I see the disabled image, but once I set the view to enabled, it won't switch to disabled again.

Implicit answered 12/9, 2012 at 7:31 Comment(0)
T
10

Try this

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

<item android:drawable="@drawable/store_item_background_selected" android:state_enabled="true" android:state_selected="true"/>
<item android:drawable="@drawable/store_item_background" android:state_enabled="true"/>
<item android:drawable="@drawable/store_item_background_disabled" android:state_enabled="false"/>

</selector>
Tonetic answered 12/9, 2012 at 7:44 Comment(0)
G
44

Although this is a really old question, one should write a selector in this order:

  1. disabled state first
  2. pressed state second
  3. normal state last
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_shortAnimTime">
    <item android:drawable="@color/due_gray" android:state_enabled="false" />
    <item android:drawable="@color/whizdm_primary_dark_color" android:state_pressed="true" />
    <item android:drawable="@color/whizdm_primary_color" />
</selector>
Graiggrail answered 4/9, 2015 at 8:58 Comment(4)
switching the order instantly fixed my problem. where did you find this information?Rooky
I had this problem for a long time using this approach: StateListDrawable res = new StateListDrawable(); res.addState(new int[]{-android.R.attr.state_enabled}, new ColorDrawable(color) ); res.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, new ColorDrawable(color)); res.addState(new int[]{}, new ColorDrawable(color)); And this worked immediately. Thanks. wish it was documented somewhere...Arron
Anytime bro. This should be documented!Graiggrail
ridiculus still works. Why is the ordner necessary ...Amary
T
10

Try this

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

<item android:drawable="@drawable/store_item_background_selected" android:state_enabled="true" android:state_selected="true"/>
<item android:drawable="@drawable/store_item_background" android:state_enabled="true"/>
<item android:drawable="@drawable/store_item_background_disabled" android:state_enabled="false"/>

</selector>
Tonetic answered 12/9, 2012 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.