How to use apply a custom drawable to RadioButton?
Asked Answered
O

5

95

It looks like we can use the following in a RadioButton:

android:button="@drawable/myCustomStateBackground"

but that drawable only occupies the spot where the radio drawable would normally go. Ideally I want my entire button background to be stateful. So when pushed, I want the entire button to look like its stuck in the pressed state. To do that, I was hoping I could do something like:

android:button="null"
android:background="@drawable/myCustomStateBackground"

but then the background drawable doesn't know about push state, like the button attribute does. Is there a way around it?

Osteotome answered 14/9, 2012 at 22:2 Comment(1)
Rather than changing the background, you're probably looking for the android:button attribute.Flavius
E
134

Give your radiobutton a custom style:

<style name="MyRadioButtonStyle" parent="@android:style/Widget.CompoundButton.RadioButton">
    <item name="android:button">@drawable/custom_btn_radio</item>
</style>

custom_btn_radio.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:state_window_focused="false"
          android:drawable="@drawable/btn_radio_on" />
    <item android:state_checked="false" android:state_window_focused="false"
          android:drawable="@drawable/btn_radio_off" />

    <item android:state_checked="true" android:state_pressed="true"
          android:drawable="@drawable/btn_radio_on_pressed" />
    <item android:state_checked="false" android:state_pressed="true"
          android:drawable="@drawable/btn_radio_off_pressed" />

    <item android:state_checked="true" android:state_focused="true"
          android:drawable="@drawable/btn_radio_on_selected" />
    <item android:state_checked="false" android:state_focused="true"
          android:drawable="@drawable/btn_radio_off_selected" />

    <item android:state_checked="false" android:drawable="@drawable/btn_radio_off" />
    <item android:state_checked="true" android:drawable="@drawable/btn_radio_on" />
</selector>

Replace the drawables with your own.

Emergency answered 14/9, 2012 at 22:21 Comment(4)
Ah sorry - this will only modify the "button" drawable - I'd like to modify the background drawable - but also have its state behave like the "button" attribute - doesn't look like RadioButton supports that?Osteotome
using this drawable once my radio button gets checked it's never get uncheckedHeadward
@Headward That's the way a RadioButton works if it's not part of a group. If you need it to be unselectable you can use a CheckBox.Moonier
It helps to me only with additional settings <style name="CustomRadioButtonStyle" parent="@android:style/Widget.CompoundButton.RadioButton"> <item name="android:button">@drawable/ic_check_selector</item> <item name="android:buttonTint">@android:color/white</item> <item name="android:buttonTintMode">multiply</item> </style>Clerestory
O
45

You should set android:button="@null" instead of "null".

You were soo close!

Organicism answered 26/6, 2013 at 9:38 Comment(0)
G
44

full solution here:

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/oragne_toggle_btn"
        android:checked="true"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/oragne_toggle_btn"
        android:layout_marginTop="20dp"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/oragne_toggle_btn"
        android:layout_marginTop="20dp"
        android:text="RadioButton" />
</RadioGroup>

selector XML

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/orange_btn_selected" android:state_checked="true"/>
<item android:drawable="@drawable/orange_btn_unselected"    android:state_checked="false"/>

 </selector>
Gamin answered 30/7, 2013 at 10:39 Comment(4)
or you could use android:button="@drawable/oragne_toggle_btn"Retrenchment
Do not use background attribute use >android:button="@drawable/oragne_toggle_btn"Tabber
What about text color?Areopagus
Old thread, I know... It looks like the above solutions android:button="@drawable/oragne_toggle_btn" or android:button="@null" and android:background="@drawable/oragne_toggle_btn" are not working when using an API < API 21. Any alternatives for API 18 that makes it possible to replace the default radiobutton image with your own?Saloon
E
12

if you want to change the only icon of radio button then you can only add android:button="@drawable/ic_launcher" to your radio button and for making sensitive on click then you have to use the selector

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/image_what_you_want_on_select_state" android:state_checked="true"/>
<item android:drawable="@drawable/image_what_you_want_on_un_select_state"    android:state_checked="false"/>


 </selector>

and set to your radio android:background="@drawable/name_of_selector"

Ence answered 26/2, 2015 at 12:46 Comment(1)
If m using android:button="@null" android:background="@drawable/myCustomStateBackground" for a radio button which is located in navigation drawer menu. It is not working properly. Some times background images are visible but sometimes not. Please help regarding same.Demission
Q
0

In programmatically, add the background image

minSdkVersion 16

RadioGroup rg = new RadioGroup(this);
RadioButton radioButton = new RadioButton(this);
radioButton.setBackground(R.drawable.account_background);
rg.addView(radioButton);
Quackery answered 14/3, 2019 at 11:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.