Change the circle color of radio button
It is simpler just setting the buttonTint color (only works on API level 21 or above):
<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 file, put your color, in this case a reddish one:
<color name="your_color">#e75748</color>
Result:
If you want to do it by 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
}
How do I change the color of radio buttons?
A radio button is a native element specific to each OS/browser. There is no way to change its color/style, unless you want to implement custom images or use a custom Javascript library which includes images (e.g. this - cached link)
Android change circle color and textColor of radio button
If you want to change the text and color of the circles at the same time, follow these steps
1.add style below
<style name="radioButton" parent="AppTheme">
<item name="colorControlNormal">#0f0</item>
<item name="colorAccent">#f00</item>
</style>
2.add this selector.xml to drawable
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#0f0" android:state_pressed="true" />
<item android:color="#f00" android:state_checked="true" />
<item android:color="#0f0" />
</selector>
3.add selector and style to RadioButton
<RadioButton
android:id="@+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:text="select"
android:textColor="@drawable/selector"
android:textSize="12sp"
android:theme="@style/radioButton"
app:useMaterialThemeColors="false" />
FULL CODE:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="16dp"
android:checkedButton="@+id/one"
android:layoutDirection="ltr"
android:orientation="horizontal">
<RadioButton
android:id="@+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:text="select"
android:textColor="@drawable/selector"
android:textSize="12sp"
android:theme="@style/radioButton"
app:useMaterialThemeColors="false" />
<RadioButton
android:id="@+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="not select"
android:textColor="@drawable/selector"
android:textSize="12sp"
android:theme="@style/radioButton"
app:useMaterialThemeColors="false" />
</RadioGroup>
And Result
Note: I used Google Material
Is it possible to change the color of selected radio button's center circle
You can mimic the radio button using some round border trick (to render the outer circle) and the pseudo-element :before
(to render the inner circle) which can fortunately be used on an input field of radio
type:
input[type='radio'] {
-webkit-appearance:none;
width:20px;
height:20px;
border:1px solid darkgray;
border-radius:50%;
outline:none;
box-shadow:0 0 5px 0px gray inset;
}
input[type='radio']:hover {
box-shadow:0 0 5px 0px orange inset;
}
input[type='radio']:before {
content:'';
display:block;
width:60%;
height:60%;
margin: 20% auto;
border-radius:50%;
}
input[type='radio']:checked:before {
background:green;
}
Working Demo.
How to to change the color of selected radio button's center circle
SCSS approach
The right way to do this is by looking at the variable $custom-radio-indicator-icon-checked
in the Compass setup => \bootstrap\scss\_variables.scss
.
$custom-radio-indicator-icon-checked: str-replace(url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'%3E%3Ccircle r='3' fill='#{$custom-control-indicator-checked-color}'/%3E%3C/svg%3E"), "#", "%23") !default;
Here you can find the use of $custom-control-indicator-checked-color
which points you to the option(s) of changing color and background-color.
$custom-control-indicator-checked-color: $component-active-color !default;
$custom-control-indicator-checked-bg: $component-active-bg !default;
Hence, changing $component-active-color
or $component-active-bg
.
$component-active-color: $white !default;
$component-active-bg: theme-color("primary") !default;
So you can change the color directly inside the variable or on component level to change all indicators of the following components:
- custom checkbox
- custom radio
- dropdown
- nav-pills
- pagination
- list-group
CSS approach
Now let's assume you don't have a SCSS setup and you're simply trying to override the color by CSS. You'll have to find the associated label's pseudo class :after
.
.custom-radio .custom-control-input:checked~.custom-control-label::after {
background-image: url(data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'%3e%3ccircle r='3' fill='%23fff'/%3e%3c/svg%3e);
}
Here you're looking for the fill
property. The hash sign (#) was replaced by %23
. Other than that it's a hexadecimal value. That's for the circle!
Changing the border-color
and background-color
for the checkbox itself is defined on the pseudo class :before
:
.custom-control-input.is-valid:checked~.custom-control-label::before,
.was-validated .custom-control-input:valid:checked~.custom-control-label::before {
border-color: #34ce57;
background-color: #34ce57;
}
Please note there's some additional weight (validation, state checked or not) to these selectors to make it work.
How to change checked color of Radio Button programmatically
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
RadioButton set fill color of the circle
You can use custom layer
<android.support.v7.widget.AppCompatRadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Text"
android:button="@drawable/selector_radio"
android:textSize="16dp" />
selector_radio.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/radio_layer" android:state_checked="true"></item>
<item android:drawable="@drawable/radio_ring_checked"></item>
</selector>
radio_layer.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/radio_ring_checked"/>
<item android:drawable="@drawable/radio_ring_unchecked"/>
</layer-list>
radio_ring_checked.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadiusRatio="4"
android:shape="ring"
android:thickness="2dp"
android:useLevel="false" >
<solid android:color="@color/ash" />
<size
android:height="30dp"
android:width="30dp" />
</shape>
radio_ring_unchecked.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadiusRatio="1000"
android:shape="ring"
android:thickness="4dp"
android:useLevel="false" >
<solid android:color="@color/colorPrimaryButton" />
<size
android:height="18dp"
android:width="18dp" />
</shape>
Related Topics
Programmatically Select Item Listview in Android
Converting JSONarray to Arraylist
How to Add a Textview to a Linearlayout Dynamically in Android
How to Pass Arraylist of Objects from One to Another Activity Using Intent in Android
Google Sign in Failed Com.Google.Android.Gms.Common.Api.Apiexception: 10:
Error Opening Trace File: No Such File or Directory (2)
Android Studio: Gradle - Build Fails -- Execution Failed for Task ':Dexdebug'
What Is the Size Limit for Logcat and How to Change Its Capacity
How to Build an APK File in Eclipse
How to Add a Custom Button State
How to Set Image Button Backgroundimage for Different State
How to Bring an Activity to Foreground (Top of Stack)
Usage of Forcelayout(), Requestlayout() and Invalidate()