13

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>
Kevin Westwood
  • 7,739
  • 8
  • 38
  • 52
  • 1
    Why are you trying to remove the drawable all together? Can you replace it with something that fits your needs? – Flynn Feb 15 '12 at 17:28
  • I want the functionality of a radio button, but want it to look like a button. Specifically I want to have two buttons side by side that toggle one another. One of the buttons appears down and the other up. Is there a better solution than where I am heading? – Kevin Westwood Feb 15 '12 at 19:13
  • 2
    I actually found that I can remove the default checkbox with the following `android:button="@null"` – Kevin Westwood Feb 15 '12 at 21:20

3 Answers3

41

Set the button value to null in layout XML

android:button="@null"
Enes
  • 2,225
  • 1
  • 19
  • 16
24

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>
Kevin Westwood
  • 7,739
  • 8
  • 38
  • 52
0

If you wanna set custom background to RadioButton programmatically, then you can set it like this,

radioButton.setButtonDrawable(null);
radioButton.setBackgroundResource(R.drawable.your_custom_drawable);
Abhishek Kumar
  • 4,532
  • 5
  • 31
  • 53