How to customize default theme of radio button in android?

Dharmesh Dhameliya picture Dharmesh Dhameliya · Feb 2, 2016 · Viewed 10.9k times · Source

i want to change default theme of radio button to custom but it's not changing the theme. please help me out

styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="android:radioButtonStyle">@style/radionbutton</item>
</style>

<!-- custom style -->
<style name="radionbutton"
     parent="Base.Widget.AppCompat.CompoundButton.RadioButton">
    <item name="android:button">@drawable/radiobutton_drawable</item>
</style>

radiobutton_drawable.xml

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

Answer

batsheva picture batsheva · Jul 27, 2017

Declare custom style in your 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 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