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

1

2 Answers 2

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*

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.