RadioButton - how to use custom available?

It looks like we can use the following in RadioButton:

android:button="@drawable/myCustomStateBackground" 

but this drawable only takes up the place where the broadcast usually goes. Ideally, I want the whole background on the button to be restrained. Therefore, when pressed, I want the entire button to look as if it is stuck in the pressed state. For this, I was hoping I could do something like:

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

but then the background drawing does not know the state of pressing, as the button attribute does. Is there any way around this?

+42
android
Sep 14
source share
4 answers

Give your radio boot your own 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 drawings with your own.

+69
Sep 14 '12 at 22:21
source share

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

You were very close!

+24
Jun 26 '13 at 9:38 on
source share

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> 
+20
Jul 30 '13 at 10:39
source share

if you want to change only the radio button icon, you can add android:button="@drawable/ic_launcher" to your switch and make a sensitive click, then you should 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 android:background="@drawable/name_of_selector" for your radio

+3
Feb 26 '15 at 12:46
source share



All Articles