Is it possible to change the radio button icon in an android radio button group

AndroidRadio Button

Android Problem Overview


I am wanting to allow the user of my android application the ability to set some parameters. The radio button is ideal for this situation. However, I don't like the radio buttons are rendered.

Is it possible to change the radio button icon? For example, is it possible to create a custom layout for each row and in that layout reference my own icon and change the font et al.

Android Solutions


Solution 1 - Android

Yes that's possible you have to define your own style for radio buttons, at res/values/styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme" parent="android:Theme">
   <item name="android:radioButtonStyle">@style/RadioButton</item>
</style>
<style name="RadioButton" parent="@android:style/Widget.CompoundButton.RadioButton">
   <item name="android:button">@drawable/radio</item>
</style>
</resources>

'radio' here should be a stateful drawable, radio.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
	<item android:state_checked="true" android:state_window_focused="false"
		android:drawable="@drawable/radio_hover" />
	<item android:state_checked="false" android:state_window_focused="false"
		android:drawable="@drawable/radio_normal" />
	<item android:state_checked="true" android:state_pressed="true"
		android:drawable="@drawable/radio_active" />
	<item android:state_checked="false" android:state_pressed="true"
		android:drawable="@drawable/radio_active" />
	<item android:state_checked="true" android:state_focused="true"
		android:drawable="@drawable/radio_hover" />
	<item android:state_checked="false" android:state_focused="true"
		android:drawable="@drawable/radio_normal_off" />
	<item android:state_checked="false" android:drawable="@drawable/radio_normal" />
	<item android:state_checked="true" android:drawable="@drawable/radio_hover" />
    </selector>

Then just apply the Custom theme either to whole app or to activities of your choice.

For more info about themes and styles look at http://brainflush.wordpress.com/2009/03/15/understanding-android-themes-and-styles/ that is good guide.

Solution 2 - Android

You can put custom image in radiobutton like normal button. for that create one XML file in drawable folder e.g

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/sub_screens_aus_hl" 
	android:state_pressed="true"/>  
<item android:drawable="@drawable/sub_screens_aus" 
	android:state_checked="true"/>  
<item android:drawable="@drawable/sub_screens_aus" 
	android:state_focused="true" />
<item android:drawable="@drawable/sub_screens_aus_dis" />  
</selector> 

Here you can use 3 different images for radiobutton

and use this file to RadioButton like:

android:button="@drawable/aus"
android:layout_height="120dp"
android:layout_width="wrap_content" 

Solution 3 - Android

The easier way to only change the radio button is simply set selector for drawable right

<RadioButton
    ...
    android:button="@null"
    android:checked="false"
    android:drawableRight="@drawable/radio_button_selector" />

And the selector is:

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

That's all

Solution 4 - Android

yes....` from Xml

android:button="@drawable/yourdrawable" 

and from Java

myRadioButton.setButtonDrawable(resourceId or Drawable);

`

Solution 5 - Android

Here's probably a quick approach,

enter image description here

With two icons shown above, you shall have a RadioGroup something like this

enter image description here

  • change the RadioGroup's orientation to horizontal
  • for each RadioButton's Properties, try giving the icon for Button under CompoundButton,
  • adjust the Padding and size,
  • and set the Background attribute when checked.

Solution 6 - Android

In case you want to do it programmatically,

checkBoxOrRadioButton.setButtonDrawable(null);
checkBoxOrRadioButton.setBackgroundResource(R.drawable.resource_name);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyamspogView Question on Stackoverflow
Solution 1 - AndroidKonstantin BurovView Answer on Stackoverflow
Solution 2 - AndroidChiragView Answer on Stackoverflow
Solution 3 - AndroidtopcblView Answer on Stackoverflow
Solution 4 - Androidshweta jariyaView Answer on Stackoverflow
Solution 5 - AndroidAung Myo LinnView Answer on Stackoverflow
Solution 6 - AndroidAbhishek KumarView Answer on Stackoverflow