I am attempting to style a radio button and so I created a selector. Everything works except the standard radio button image still shows up. How do I remove the default radio button checkbox. I tried assigning the drawable to an empty string, but the compiler does not like it.
Here is the code of one of my selector states:
<item android:state_checked="true" android:drawable="">
<shape>
<gradient
android:startColor="#808080"
android:centerColor="#2F2F2F"
android:endColor="#2F2F2F"
android:angle="270" />
<stroke
android:width="2dp"
android:color="@color/black" />
<corners
android:radius="5dp" />
</shape>
</item>
You can select a different default option by clicking the radio button next to a different choice, or turn off the default choice entirely by clicking the active radio button. Note that without a default choice, no answer is still a valid choice by the user unless you specifically mark the field as required.
To set a radio button to checked/unchecked, select the element and set its checked property to true or false , e.g. myRadio. checked = true .
Give people control and align with their expectations (Good): It is better to have a selected radio button by default, given that people cannot deselect and set the button back to its original state once one has been selected. A default selection sets the correct user expectation.
When you select an item in a radio button list, you can't deselect it. You can select something else and change your selection, but you can't un-answer the question once you have selected an answer.
Set the button value to null in layout XML
android:button="@null"
I found that I cannot remove the default radiobutton checkbox in the selector. I found that it can be removed by assigning the "button" property to null, in either the style or the definition of the radiobutton itself.
Here is an example using a style.
<RadioButton
android:id="@+id/myRadioButton"
style="@style/RadioButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/custom_radio_button"/>
<style name="RadioButtonStyle" parent="@android:style/Widget.CompoundButton">
<item name="android:background">@drawable/radiobutton_selector</item>
<item name="android:button">@null</item>
</style>
radiobutton_selector defines a selector that draws the button in its different checked states. Here is the checked state:
<item android:state_checked="true">
<shape>
<gradient
android:startColor="@color/titleButtonCheckedGradientLight"
android:centerColor="@color/titleButtonCheckedGradientDark"
android:endColor="@color/titleButtonCheckedGradientDark"
android:angle="270" />
<stroke
android:width="1dp"
android:color="@color/titleButtonBorder" />
<corners
android:radius="5dp" />
</shape>
</item>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With