4

I would like to know if its possible programmatically and how to change programmatically the color of RadioButton when it checked ?

PS : I dont want to use XML

in XML I use something like this and its work :

    <RadioButton
        android:id="@+id/radio_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test1"
        android:textColor="@color/red"
        android:textSize="16dp"
        android:paddingStart="10dp"
        android:paddingEnd="0dp"
        android:theme="@style/CustomColorRadioButton" />

In my style.xml

<style name="CustomRadioButton" parent="AppTheme">
    <item name="colorControlActivated">@color/blue</item>
</style>

How can I do that programmatically ?

Tai Nguyen
  • 906
  • 1
  • 10
  • 30

1 Answers1

9

Try this:

ColorStateList colorStateList = new ColorStateList(
        new int[][]{
                new int[]{-android.R.attr.state_enabled}, //disabled
                new int[]{android.R.attr.state_enabled} //enabled
        },
        new int[] {
                Color.BLACK, //disabled
                Color.BLUE //enabled
        }
    );

radio.setButtonTintList(colorStateList);

See: Change Circle color of radio button- Android

svkaka
  • 3,942
  • 2
  • 31
  • 55
Anton Sarmatin
  • 505
  • 2
  • 8