5

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>

Dharmesh Dhameliya
  • 174
  • 1
  • 3
  • 10

6 Answers6

10

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

batsheva
  • 2,175
  • 1
  • 20
  • 32
2

Finally, i found answer by myself

I just create custom layout and add to ListAdapter object

radiobuttonlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/checkedTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="@drawable/radiobutton_drawable"
android:gravity="center_vertical"
android:text="New CheckedTextView"
android:padding="@dimen/dp10"
android:textColor="@color/white" />

Java file code

ListAdapter listAdapter = new ArrayAdapter<String>    (context,R.layout.radiobuttonlayout,aryStrLockscreens);
    listview_lockscreen.setAdapter(listAdapter);

It's worked for me.

Dharmesh Dhameliya
  • 174
  • 1
  • 3
  • 10
1

The radiobutton_drawable.xml has a missing </selector> tag in the end. Plus you should do this to your radio button-

<RadioButton android:layout_width="wrap_content"
           android:layout_marginTop="20dp"
           android:id="@+id/radio"
           style="@style/radionbutton"
           android:layout_height="wrap_content"/>
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Vaibhav Sharma
  • 2,293
  • 1
  • 17
  • 24
1

Sure you can do it in the parent AppTheme just do the following: First you will Use the MaterialTheme as your parent theme:

<style name="AppTheme" parent="Theme.MaterialComponents.*">

Then create your RadioButton style in res/values/styles

<style name="Widget.App.RadioButton" parent="Widget.MaterialComponents.CompoundButton.RadioButton">
        <item name="buttonTint">@color/white</item>
       //put any thing here
</style>

Add it to Parent AppTheme

<style name="AppTheme" parent="Theme.MaterialComponents.*">
<item name="radioButtonStyle">@style/Widget.App.RadioButton</item>
</style>

Then Use it in your layout like:

<com.google.android.material.radiobutton.MaterialRadioButton
            android:id="@+id/rb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hi"
            />

for more details about theming Theming RadioButton

Mahmoud Ayman
  • 1,157
  • 1
  • 17
  • 27
0

Need to apply individual style to radio button

<RadioButton
        android:layout_width="wrap_content"
        style="@style/radiobutton_drawable"
        android:layout_height="wrap_content"/>
Suhas Bachewar
  • 1,230
  • 7
  • 21
0

You could add this to your styles.xml

<style name="Widget.Styles.Radio.RadioButton" 
    <item name="android:textAppearance">@style/ANY_TYPOGRAPHY_STYLE</item>
</style>

ANY_TYPOGRAPHY_STYLE is for example:

  • TextAppearance.MaterialComponents.Caption if you are using the Material theme
  • TextAppearance.AppCompat.Caption if you are using AppCompat theme.

  • OR any typography style you have created

Example:

<style name="TextAppearance.TypographyStyles.Body2" parent="TextAppearance.MaterialComponents.Body2">
    <item name="fontFamily">@font/poppins_regular</item>
    <item name="android:fontFamily">@font/poppins_regular</item>
    <item name="android:textSize">14sp</item>
</style>

And use the radio button style as a style for the MaterialRadioButton or RadioButton

<com.google.android.material.radiobutton.MaterialRadioButton
            style="@style/Widget.Styles.Radio.RadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TEXT"/>