Can't change Radio Button color on Android

31,066

Solution 1

After reading @Ranjith's answer i did a little digging and this seemed to work. Just add it to your AppTheme.

    //red is the color of the pressed state and activated state
    <item name="colorControlActivated">@android:color/holo_red_light</item>
    //black is the color of the normal state
    <item name="colorControlNormal">@android:color/black</item>

My question is how do you do this programatically as I have dynamic radio buttons??

Solution 2

This can be done in two ways (to support pre-Lollipop):

  1. Use AppCompatRadioButton:

    AppCompatRadioButton radioButton;
    // now use following methods to set tint colour
    radioButton.setSupportButtonTintMode();
    radioButton.setSupportButtonTintList();
    
  2. Apply this as style to your RadioButton in your XML:

    <style name="RadioButton.Login" parent="Widget.AppCompat.CompoundButton.RadioButton">
        <item name="android:textColor">@android:color/white</item>
        <item name="buttonTint">@android:color/white</item>
    </style>
    

Solution 3

RadioButton raPrivate = (RadioButton) layout.findViewById(R.id.radioPrivate);
int textColor = Color.parseColor(#000000);
raPrivate.setButtonTintList(ColorStateList.valueOf(textColor));

Solution 4

If you want to change the color, but not change colorControlActivated and colorControlNormal for the entire app, you can override your apptheme just for the radio button by creating a new style:

<android.support.v7.widget.AppCompatRadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_alignParentEnd="true"
    android:theme="@style/RadioButtonTheme"
    android:id="@+id/radioButton"/>



<style name="RadioButtonTheme" parent="@style/AppTheme">
    <item name="colorControlActivated">@color/colorAccent</item>
    <item name="colorControlNormal">@color/colorPrimaryDark</item>
</style>

Solution 5

No need of additional styling. Android supports it via xml. Just add android:buttonTint="@color/yourColor" in your radio button.

For eg.

<RadioButton
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:buttonTint="@color/red"
 android:text="Upload"
 android:textColor="@color/text_dark_gray"
 android:textSize="14sp" />
Share:
31,066
Freguglia
Author by

Freguglia

Updated on December 01, 2020

Comments

  • Freguglia
    Freguglia over 3 years

    I'm using Android Studio. I need to change the color of the Radio Button, after changing the Button Tint Color value to the one I need it works on the preview, but whenever I launch the app on a device the button is the standard green/blue-ish color.

    Is this some kind of device API level issue? If so, is it possible to change the color for older devices?

  • AdamMc331
    AdamMc331 almost 9 years
    It looks like the buttonTint is only used in API 21 and up. Any idea for what to use before that?
  • Psypher
    Psypher almost 9 years
    @victor freg mark answered if my answer helped you so that it helps other users
  • Sufian
    Sufian over 7 years
    @McAdam331 please see my answer below.
  • AdamMc331
    AdamMc331 over 7 years
    Nice answer, thanks for including the backward compatibility support.
  • voghDev
    voghDev almost 7 years
    after various failed answers, this answer plus AppCompatRadioButton is THE answer. Thanks
  • sud007
    sud007 almost 7 years
    Superb! setSupportButtonTintList Works like a charm on API 21 and above.