1

How can I do this?

enter image description here

This is what I use to change text color:

maleRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    maleRadioButton.setTextColor(getApplicationContext().getResources().getColor(R.color.colorPrimary));
                } else {
                    maleRadioButton.setTextColor(getApplicationContext().getResources().getColor(R.color.lightGray));
                }
            }
        });

and I set <item name="android:buttonTint">@color/colorAccent</item>, but the unselected radio button is colorAccent instead of lightGray

Any ideas?

y07k2
  • 1,898
  • 4
  • 20
  • 36

2 Answers2

5

To change text color when select/checked/focused use this colorList in android:textColor for widget:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/color1" android:state_focused="true" android:state_pressed="false"/>
    <item android:color="@color/color1" android:state_focused="true" android:state_pressed="true"/>
    <item android:color="@color/color1" android:state_focused="false" android:state_pressed="true"/>
    <item android:color="@color/color1" android:state_checked="true"/>
    <item android:color="@color/color2"/>
</selector>

and for background of button use this in android:background:

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

Note the usage of color1 as checked_state in text and as unchecked_state for background to make contrast..

I hope that may help,'.

Maher Abuthraa
  • 17,493
  • 11
  • 81
  • 103
3

custom_radio_button.xml

<?xml version="1.0" encoding="utf-8"?>

    <selector xmlns:android="http://schemas.android.com/apk/res/android">

         <item android:state_checked="true" android:drawable="@drawable/checkedradiobutton" />
         <item android:state_checked="false" android:drawable="@drawable/unchekedradiobutton" />

    </selector>

Use two drawables for selected and not selected state of the Radio Buttons

and use the above selector as:

<!--   Customized RadioButtons  -->


              <RadioButton
                   android:id="@+id/radioButtonCustomized1"
                   android:layout_width="fill_parent"
                   android:layout_height="wrap_content"
                   android:text="Radio Button Selected"
                   android:layout_marginTop="20dp"
                   android:checked="true"
                   android:button="@drawable/custom_radio_button"
                   android:textSize="20dp" />

             <RadioButton
                   android:id="@+id/radioButtonCustomized2"
                   android:layout_width="fill_parent"
                   android:layout_height="wrap_content"
                   android:text="Radio Button Not Selected"
                   android:layout_marginTop="10dp"
                   android:checked="false"
                   android:button="@drawable/custom_radio_button"
                   android:textSize="20dp" />
kgandroid
  • 5,507
  • 5
  • 39
  • 69