Selector - Item order


#1

When I have the following (the books example) it works. However, when I reverse the order of the “items” where I have normal before pressed it does NOT work?!? Why? It seems like the order should not be important.

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

This order does NOT work.

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

#2

A state list drawable will always use the first item that matches the current object state. In your second example, the first item will always match (since it has no conditions), and thus the selector will never even look at the second item.

If you really want to place the default case first, you can add android:state_pressed="false". However, this might make it slightly trickier to add more states if you want to do that later.