0

I am new to Android. I need to implement a radio button functionality with custom images. Is there any built in controls for Radio button or any easy way to implement this feature?

Thanks in advance

sree_iphonedev
  • 3,514
  • 6
  • 31
  • 50
  • refer this one .. hope it vill be helpful to you http://stackoverflow.com/q/3576507/1021695 – KMI Jun 04 '12 at 11:38

2 Answers2

3

make drawable folder and create button_radio.xml file:

res/drawable/button_radio.xml

button_radio.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="true" android:state_pressed="false"
      android:drawable="@drawable/radio_on"/>
  <item android:state_checked="false" android:state_pressed="false"
      android:drawable="@drawable/radio_off"/>
  <item android:state_checked="true" android:state_pressed="true"
      android:drawable="@drawable/radio_on_pressed"/>
  <item android:state_checked="false" android:state_pressed="true"
      android:drawable="@drawable/radio_off_pressed"/>
</selector>

in your layout set radio button:

main.xml:

<RadioGroup android:layout_width="fill_parent"
   android:layout_height="50dp"
   android:orientation="horizontal"
   android:checkedButton="@+id/first">
   <RadioButton android:id="@+id/first"
      android:width="50dp"
      android:height="50dp"
      android:button="@drawable/button_radio"/>
   <RadioButton android:id="@+id/second"
      android:width="50dp"
      android:height="50dp"
      android:button="@drawable/button_radio"/>
   <RadioButton android:id="@+id/third"
      android:width="50dp"
      android:height="50dp"
      android:button="@drawable/button_radio"/>
   <RadioButton android:id="@+id/fourth"
      android:width="50dp"
      android:height="50dp"
      android:button="@drawable/button_radio"/>
</RadioGroup>

Note *radio_on & radio_off will be your custom image*

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
Andy
  • 5,379
  • 7
  • 39
  • 53
0

your radio button should have this code for properly setting background

<RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@android:color/transparent"
            android:background="@drawable/radiobutton_selector" />

this is your selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/radio_button" android:state_checked="false"/>
    <item android:drawable="@drawable/radio_button_h" android:state_checked="true"/>
    <item android:drawable="@drawable/radio_button"/> <!-- default -->

</selector>

radio_button_h and radio_button is drawable images . it works for me