1
<item android:state_enabled="false" android:color="@android:color/bright_foreground_dark_disabled"/>
<item android:color="@android:color/bright_foreground_dark"/>

What is the difference between these two? According to the documentation the color of the first item is used when the state is NOT enabled, and the second is the default one. So, if the item is not enabled, which color is used?

2 Answers2

3

If the item is not enabled the first item is used, as it matches all its state selectors. Selector items are checked from top to bottom, and the first one for which the state matches is used.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • 1
    Yes. Just read from the docs: "During each state change, the state list is traversed top to bottom and the first item that matches the current state is used—the selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state." Thanks – NullPointerException Dec 19 '12 at 19:58
0

false states are intended to be used in combination with other states. For example, you have checkable item, and it can be either disabled or enabled, and you want to have different drawables for each state combination. This can be achieved the following way:

<item android:state_checked="true" android:state_enabled="true" android:drawable="@drawable/drawable1"/>
<item android:state_checked="true" android:state_enabled="false" android:drawable="@drawable/drawable2"/>
<item android:state_checked="false" android:state_enabled="true" android:drawable="@drawable/drawable3"/>
<item android:state_checked="false" android:state_enabled="false" android:drawable="@drawable/drawable4"/>
<item android:drawable="@drawable/drawable0"/>

If you don't need such combinations, there's no need to use state_xxx="false" without other states, though that's not a mistake.

Andrii Chernenko
  • 9,873
  • 7
  • 71
  • 89