Update: 1. use this instead
<android.support.v7.widget.AppCompatRadioButton android:id="@+id/rbtn_test" android:layout_width="wrap_content" android:layout_height="wrap_content" app:buttonTint="@color/primary" />
2. Then add this line to the parent layout or Alt + Enter in Android Studio to automatically add xmlns:app="http://schemas.android.com/apk/res-auto"
A minimal example should look like this:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.AppCompatRadioButton android:id="@+id/rbtn_test" android:layout_width="wrap_content" android:layout_height="wrap_content" app:buttonTint="@color/primary" /> </LinearLayout>
3. In your program you need to call like this. AppCompatRadioButton radioButton = (AppCompatRadioButton) view.findViewById(R.id.rbtn_test);
In principle, this type of template can be applied to all types of AppCompact, such as AppCompatCheckBox, AppCompatButton, etc.
Old answer:
To support the android API 21, you can use AppCompatRadioButton. . Then use the setSupportButtonTintList method to change the color. This is my piece of code for creating the switch.
AppCompatRadioButton rb; rb = new AppCompatRadioButton(mContext); ColorStateList colorStateList = new ColorStateList( new int[][]{ new int[]{-android.R.attr.state_checked}, new int[]{android.R.attr.state_checked} }, new int[]{ Color.DKGRAY , Color.rgb (242,81,112), } ); rb.setSupportButtonTintList(colorStateList);
Tested result in API 19:

For more details see the android link link .
aknay Dec 29 '15 at 14:56 2015-12-29 14:56
source share