Axarydax Axarydax - 1 year ago 108
Android Question

Android RadioButton textColor selector

I have a selector for

of a
like this:

<selector xmlns:android="">
<item android:state_selected="true" android:color="#fff"/>
<item android:state_focused="true" android:color="#f00"/>
<item android:state_pressed="true" android:color="#0f0"/>
<item android:state_focused="false" android:state_pressed="false" android:color="#00f"/>

I expected that the one selected
will have different color than the others.

However, all of the
s have blue text (using android:state_focused="false" android:state_pressed="false"), even the one that is selected.

What am I doing wrong?

Answer Source

It looks like you're just using the wrong selectors. The docs describe selecting as follows:

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.

Source link

So, in order:

  1. state_selected is never true as RadioButtons use state_checked when checked.
  2. state_focused is never called because RadioButton will never receive input focus.
  3. state_pressed should be working. When you hold your finger down you don't see the text appearing green?
  4. state_focused false and state_pressed false ends up being default so you see blue.

If you would like to see different states, try these:

<selector xmlns:android="">
    <item android:state_pressed="true" android:color="#0f0"/>
    <item android:state_checked="true" android:color="#fff"/>
    <item android:color="#00f"/>

I have tested the above and can see all colors being expressed appropriately.