Tuesday, December 13, 2011

Android Custom Radio Button


The files which will be needed to customize Radio Button are as follows
In res/drawable directory:
  1.  radio.xml
Have two images for radio on and off.
Code snippet for Radio.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:drawable="@drawable/radio_on" android:state_checked="true" android:state_pressed="false"/>
    <item android:drawable="@drawable/radio_off" android:state_checked="false" android:state_pressed="false"/>
    <item android:drawable="@drawable/radio_on" android:state_checked="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/radio_off" android:state_checked="false" android:state_pressed="true"/>
</selector>

To replace the radio button  style, we need to set the selector at background instead of button and set the button to null.
Change the radio button code as follows.
<RadioGroup android:id="@+id/radioGrp"  android:layout_width="fill_parent"
      android:layout_height="wrap_content" android:gravity="center"
      android:orientation="horizontal" >
<RadioButton android:id="@+id/auto" android:background="@drawable/radio"
            android:button="@null" android:checked="true"
            android:text="Auto" />
<RadioButton android:id="@+id/manual" android:background="@drawable/radio"
            android:button="@null" android:checked="false"
            android:text="Manual" />
</RadioGroup>

Output:

 

1 comment :

  1. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
    html5 development| flash to html5 converter

    ReplyDelete