sree_iphonedev
sree_iphonedev

Reputation: 3534

Radio button control in Android

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

Upvotes: 0

Views: 2947

Answers (2)

Ashish Mishra
Ashish Mishra

Reputation: 29

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

Upvotes: 0

Andy
Andy

Reputation: 5379

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*

Upvotes: 3

Related Questions