Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use apply a custom drawable to RadioButton?

Tags:

android

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?

like image 637
user291701 Avatar asked Sep 14 '12 22:09

user291701


People also ask

How do I add radio buttons to my radio group?

List<String> list = new ArrayList<String>(); list. add("one"); list. add("two"); list. add("three"); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.

How do I add a radio button to a radio group dynamically?

For creating dynamic RadioButton, we need to use android. view. ViewGroup. LayoutParams which configures the width and height of views and implements setOnCheckedChangeListener() method of RadioGroup class.

How can I get RadioButton text in Android?

To get the selected radio button, we have used radioGroup. getCheckedRadioButtonId() method, which returns the id of the selected radio button. Then to get the text of the selected radio button, we have used getText() method on that selected radio button.


2 Answers

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.

like image 151
Benito Bertoli Avatar answered Sep 30 '22 22:09

Benito Bertoli


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

You were soo close!

like image 43
Tore Rudberg Avatar answered Sep 30 '22 22:09

Tore Rudberg