Change circle color of radio buttons

I want to change the color of the RadioButton circle, I could not figure out which property to set. My background color is black, so it becomes invisible. I want to set the circle color to white.

+124
android android-radiobutton
Jun 15 '13 at 4:34 on
source share
19 answers

Simply put, just set the color of buttonTint: (only works at API level 21 or higher)

<RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radio" android:checked="true" android:buttonTint="@color/your_color"/> 

in your values ​​/colors.xml put your color in this case reddish:

 <color name="your_color">#e75748</color> 

Result:

Colored Android Radio Button

If you want to do this with code (also API 21 and above):

 if(Build.VERSION.SDK_INT>=21) { 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);//set the color tint list radio.invalidate(); //could not be necessary } 
+209
Apr 09 '15 at 23:50
source share

This site is really good for customizing Android components in general: http://android-holo-colors.com/

Just select the radio button, make the color white, download and make a profit!

Additional Information

If after that you do not see your requested change. Then you need to make sure "What is your theme" applied in AndroidManifest.xml .

Also see that you apply any style to activity ?

just clean these things and it will work 100% correctly.

+29
Jun 15 '13 at 4:37 on
source share

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:

This test is tested on API 19

For more details see the android link link .

+149
Dec 29 '15 at 14:56
source share
 <android.support.v7.widget.AppCompatRadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" app:buttonTint="@color/Color" /> 
+72
Mar 04 '16 at 16:24
source share

Work with API up to 21, and also message 21. In styles.xml put:

 <!-- custom style --> <style name="radionbutton" parent="Base.Widget.AppCompat.CompoundButton.RadioButton"> <item name="android:button">@drawable/radiobutton_drawable</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">false</item> <item name="android:backgroundDimEnabled">true</item> </style> 

Your radio button in xml should look like this:

 <RadioButton android:layout_width="wrap_content" style="@style/radionbutton" android:checked="false" android:layout_height="wrap_content" /> 

Now you need to make radiobutton_drawable.xml in the drawable folder . Here is what you need to insert:

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

Your radio_unchecked.xml :

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <stroke android:width="1dp" android:color="@color/colorAccent"/> <size android:width="30dp" android:height="30dp"/> </shape> 

Your radio_checked.xml :

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="oval"> <stroke android:width="1dp" android:color="@color/colorAccent"/> <size android:width="30dp" android:height="30dp"/> </shape> </item> <item android:top="5dp" android:bottom="5dp" android:left="5dp" android:right="5dp"> <shape android:shape="oval"> <solid android:width="1dp" android:color="@color/colorAccent"/> <size android:width="10dp" android:height="10dp"/> </shape> </item> </layer-list> 

Just replace @color/colorAccent with the color of your choice.

+44
Feb 23 '16 at 15:11
source share

You should use this code:

 <android.support.v7.widget.AppCompatRadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:buttonTint="@color/black" android:text="Radiobutton1" app:buttonTint="@color/black" /> 

Using app:buttonTint instead of android:buttonTint as well as android.support.v7.widget.AppCompatRadioButton instead of Radiobutton !

+14
May 04 '17 at 15:57
source share

The question is old, but I think my answer will help people. You can change the color of the radio button without a mark and check the status using the style in xml.

 <RadioButton android:id="@+id/rb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:theme="@style/RadioButtonStyle" /> 

In style.xml

 <style name="RadioButtonStyle" parent="Theme.AppCompat.Light"> <item name="colorAccent">@android:color/white</item> <item name="android:textColorSecondary">@android:color/white</item> </style> 

You can set the desired colors in this style.

+10
Jan 14 '17 at 10:24 on
source share

Set the buttonTint property. For example, android:buttonTint="#99FF33" .

+9
Feb 21 '15 at 3:45
source share

I did it shortly like this (work on API up to 21 as well as post 21)

Your xml switch should look like this:

  <RadioButton android:id="@+id/radioid" android:layout_height="wrap_content" android:layout_width="wrap_content" android:button="@drawable/radiodraw" /> 

in radiodraw.xml

  <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="false" > <shape android:shape="oval" > <stroke android:width="1dp" android:color="#000"/> <size android:width="30dp" android:height="30dp"/> <solid android:color="@android:color/transparent"/> </shape> </item> <item android:state_checked="true"> <layer-list> <item> <shape android:shape="oval"> <stroke android:width="1dp" android:color="#000"/> <size android:width="30dp" android:height="30dp"/> <solid android:color="@android:color/transparent"/> </shape> </item> <item android:top="5dp" android:bottom="5dp" android:left="5dp" android:right="5dp"> <shape android:shape="oval"> <solid android:width="1dp" android:color="#000"/> <size android:width="10dp" android:height="10dp"/> </shape> </item> </layer-list> </item> </selector> 

you need to add color transparent to draw an unchecked state, and also draw a solid black oval.

+6
Jan 07 '17 at 0:03
source share

There is an xml attribute for it:

 android:buttonTint="yourcolor" 
+6
Apr 28 '17 at 10:55
source share

For by API 21

Create your own RadioButton style.xml style

  <style name="RadioButton" parent="Theme.AppCompat.Light"> <item name="colorAccent">@color/green</item> <item name="android:textColorSecondary">@color/mediumGray</item> <item name="colorControlNormal">@color/red</item> </style> 

In the layout, use the theme:

 <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:theme="@style/RadioButton" /> 



For API 21 and more

Just use buttonTint

  <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:buttonTint="@color/green" /> 
+5
Oct. 14 '18 at 12:20
source share

Sometimes you just need to override colorControlNormal as follows:

  <style name="RadioButtonStyle" parent="AppTheme"> <item name="colorControlNormal">@color/pink</item> <item name="colorAccent">@color/colorPrimary</item> <item name="android:textColorSecondary">@color/black</item> </style> 

And you get a button like this:

enter image description here

colorControlNormal is used for uncontrolled state and colorAccent for marked ones.

+4
Jan 21 '17 at 21:52
source share
  • Declare a custom style in the styles.xml file.

     <style name="MyRadioButton" parent="Theme.AppCompat.Light"> <item name="colorControlNormal">@color/indigo</item> <item name="colorControlActivated">@color/pink</item> </style> 
  • Apply this style to your RadioButton via the android: theme attribute.

     <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="Radio Button" android:theme="@style/MyRadioButton"/> 

    only if your activity extends AppCompatActivity

+4
Jul 27 '17 at 11:29 on
source share
 <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radio" android:buttonTint="@color/my_color"/> 

The "All" button will change color, circle field and central check.

+1
Oct 13 '15 at 19:38
source share

RadioButton defaults to colorAccent in res / values ​​/colors.xml. So go to this file and change the value

<color name="colorAccent">#3F51B5</color>

to the color you want.

+1
Jan 10 '17 at 9:58 on
source share

The easiest way is to change the color of colourAccent to values->colours.xml
but keep in mind that this will also change other things, such as editing the color of the text cursor, etc.

< color name="colorAccent">#75aeff</color >

0
Aug 24 '17 at 8:57
source share

If you want to set a different color for the switch with and without clicking, just use:

  android:buttonTint="@drawable/radiobutton" in xml of the radiobutton and your radiobutton.xml will be: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="#1E88E5"/> <item android:state_checked="true" android:color="#00e676"/> <item android:color="#ffffff"/> 

0
Feb 20 '19 at 9:56
source share

just use android:buttonTint="@color/colorPrimary" for the tag, hope this helps

0
Apr 28 '19 at 0:57
source share

@ jh314 is true. In AndroidManifest.xml

  <application android:allowBackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/AppTheme"></application> 

In style.xml

  <!-- Application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorAccent">@color/red</item> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style> 

The item name must be colorAccent, it defines the color of the default application widget.

But if you want to change the color in the code, perhaps @aknay's answer is correct.

-four
Mar 25 '16 at 2:10
source share



All Articles