37

I have a selector for textColor of a RadioButton like this:

<selector xmlns:android="http://schemas.android.com/apk/res/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"/>
</selector>

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

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

What am I doing wrong?

Axarydax
  • 16,353
  • 21
  • 92
  • 151

5 Answers5

76

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="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#0f0"/>
    <item android:state_checked="true" android:color="#fff"/>
    <item android:color="#00f"/>
</selector>

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

Grant Amos
  • 2,256
  • 17
  • 11
  • setting this selector as background for RadioButton will it work ?. I am not able set as RadioButton background. – Ramesh_D Jun 25 '14 at 06:00
  • Yes, setting this as your background should work. If the above were in "drawable/radiobutton_background.xml" then you would add it as follows: "android:background="@drawable/radiobutton_background". – Grant Amos Jun 25 '14 at 14:56
  • @Ramesh_D Yes it is. `#778899` can also be `#789`. Values just take double places when rendered. – sud007 Dec 06 '17 at 06:02
30

According to Android. https://developer.android.com/guide/topics/resources/color-list-resource.html. https://developer.android.com/reference/android/content/res/ColorStateList.html

You have to create a folder called 'color' in 'res' directory and create a new file called radiobuttonstate.xml for example which it looks like this.

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_focused="true"
    android:color="YOUR COLOR" />

   <item
    android:state_pressed="true"
    android:state_enabled="false"
    android:color="YOUR COLOR" />

    <item android:color="YOUR COLOR"
    android:state_checked="true"/>

    <item
    android:state_enabled="false"
    android:color="YOUR COLOR" />

   <item android:color="YOUR COLOR" />
</selector>

then in your radio button define in the android:textColor attribute your color list you previously defined.

  <RadioButton
                    android:id="@+id/radio_H"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:text="@string/string_example"
                    android:textColor="@color/radiobuttonstate"
                    android:textAlignment="center" />
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Isaias Carrera
  • 675
  • 9
  • 9
15

The answer provided by @GrantAmos is perfect and working. If you want to text color selector through XML, please use this code.

android:textColor="@color/textview_selector"

However, if you want to set the selector programmatically, use this code -

radioButton.setTextColor(ContextCompat.getColorStateList(getContext(), R.color.textview_selector));

Hope it will save someone's time.

Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107
4
    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@color/dark_grey"/>
    <item android:state_checked="true" android:drawable="@color/topic_green"/>
</selector>

This one works for me. Actually when i use android:color="@color/dark_grey". It didn't work. But when i changed to drawable it did.

Ramesh_D
  • 689
  • 1
  • 7
  • 25
-1
<style name="MyRadioButton" parent="Theme.AppCompat.Light">
    <item name="colorControlNormal">@color/lbl_login</item>
    <item name="colorControlActivated">@color/btn_login_back_color</item>
</style>

 <RadioButton
        android:id="@+id/btn_radio_credit_card"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_8sdp"
        android:fontFamily="@font/poppins_regular"
        android:paddingStart="@dimen/_14ssp"
        android:paddingEnd="@dimen/_1sdp"
        android:text="Credit Card"
        android:textColor="@color/lbl_login"
        android:textSize="@dimen/_14ssp"
        android:theme="@style/MyRadioButton" />

Refrence