There are times when you find yourself in a state where things are out of control just like a RadioButton.
Weird behaviours crop out while you try to add a radio button to your layout dynamically or inflated from layout.
Some times the button's drawables doesn't appear at all, sometimes it appears unexpected.
So for such a scenario, there is a cure in few steps as follows:
1. Try to use AppCompatRadioButton from support library. Set a custom style to it which extends from "@android:style/Widget.Holo.CompoundButton.RadioButton"
<style name="RadioButtonStyle" parent="@android:style/Widget.Holo.CompoundButton.RadioButton"> <item name="buttonTint">@color/coke_red</item> <item name="android:textSize">@dimen/dialog_common_transaction_item_details_text_view</item> <item name="android:typeface">normal</item> <item name="android:button">@drawable/custom_radio_selector</item> </style>
2. Add two image drawables, one for checked and other for unchecked state.
3. Create a custom drawable selector "custom_radio_selector.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/ic_radio_on" /> <item android:state_checked="false" android:state_window_focused="false" android:drawable="@drawable/ic_radio_off" /> <item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/ic_radio_on" /> <item android:state_checked="false" android:state_pressed="true" android:drawable="@drawable/ic_radio_off" /> <item android:state_checked="true" android:state_focused="true" android:drawable="@drawable/ic_radio_on" /> <item android:state_checked="false" android:state_focused="true" android:drawable="@drawable/ic_radio_off" /> <item android:state_checked="false" android:drawable="@drawable/ic_radio_off" /> <item android:state_checked="true" android:drawable="@drawable/ic_radio_on" /> </selector>
where ic_radio_off.png and ic_radio_on.png are the image drawbles added as required.
Demo: