Custom Radio Buttons

Introduction
A RadioGroup class is used for set of radio buttons. If we check one radio button that belongs to a radio group, it automatically unchecks any previously checked radio button within the same group.

we will use “android.widget.RadioButton” class to render radio button, and those radio buttons are usually grouped by android.widget.RadioGroup. If RadioButtons are in group, when one RadioButton within a group is selected, all others are automatically deselected.

Normally radio button icon is diaplayed in before text.in custom radio button that icon will display in right of the text

demo




Step 1
  • Disable inbuild radio button icon
   android:button="@null"

Step 2
  •  Then use custom radio button images


Using this code

activity_main.xml

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/profile_info_gender"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
android:background="#F7FE2E"
android:weightSum="2" >

<RadioButton
android:id="@+id/profile_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:checked="true"
android:drawablePadding="5dp"
android:drawableRight="@drawable/radio"
android:background="#F7FE2E"
android:text="Male"
android:textColor="#FF0000" />

<RadioButton
android:id="@+id/profile_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:button="@null"
android:drawablePadding="5dp"
android:drawableRight="@drawable/radio"
android:background="#F7FE2E"
android:text="Female"
android:textColor="#FF0000" />

</RadioGroup>


radio.xml

This xml fils placed under drawable folder for onclick image change

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@drawable/select" />
<item android:state_checked="false"
android:drawable="@drawable/unselect" />
</selector>



 


Previous
Next Post »

1 comments:

Write comments
Anonymous
AUTHOR
November 9, 2017 at 11:48 AM delete

Hmm is anyone else experiencing problems with the pictures on this
blokg loading? I'm trying to determine if itts a problem on my end or if it's
thhe blog. Any feedback would be greatly appreciated.

Reply
avatar