Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove default radio button checkbox when using a custom selector

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>
like image 943
Kevin Westwood Avatar asked Feb 15 '12 17:02

Kevin Westwood


People also ask

How can I remove default selection from radio button?

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.

How do I uncheck a radio button?

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 .

Should radio buttons have a default selection?

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.

Can you deselect a radio button UX?

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.


2 Answers

Set the button value to null in layout XML

android:button="@null"
like image 170
Enes Avatar answered Sep 28 '22 14:09

Enes


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>
like image 22
Kevin Westwood Avatar answered Sep 28 '22 12:09

Kevin Westwood