Custom icon for a radio button
Asked Answered
J

3

10

I am trying to implement a custom icon for the radio button in android. i.e. I want the Radio button icon to be custom one of my choice. I am able to do that with android:button property inside the radio button but I am not getting on how to define it when the user clicks it. In my application when the user clicks it nothing happens. I have also followed this guide here.

I have 2 images :
 A. tire.png (When the radio button is not clicked)
 B. tireinvert.png (When the radio button is clicked)

My RadioButton.xml file :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.techfrk.customizeradiobutton.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

     <RadioGroup
                android:id="@+id/newexprgrp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <RadioButton
                android:id="@+id/tvllocrbtn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="40dp"
                android:text="Travelling Expense (Local)"
                />

                  <RadioButton
                android:id="@+id/tvloutrbtn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="15dp"
                android:text="Travelling Expense (Outstation)"

                 />

               <RadioButton
                android:id="@+id/phnexprbtn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="15dp"
                android:text="Phone Expense"

                 />

                   <RadioButton
                android:id="@+id/miscexprbtn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="15dp"
                android:text="Misc Expense"

                 />
            </RadioGroup>

</RelativeLayout>

Can anyone please guide me ?

Juglandaceous answered 2/4, 2015 at 11:25 Comment(0)
K
26

Right click on your drawable folder then->new->android xml file->choose root element "selector" then paste code below in the file:

<item android:drawable="@drawable/tire" android:state_checked="true"/>
  <item android:drawable="@drawable/tireinvert" android:state_checked="false"/>

then in xml of your radio button add this line:

android:button="@drawable/radio"

here radio is the selector xml file which we have created in the drawable folder

Kolyma answered 2/4, 2015 at 11:37 Comment(0)
S
7

//first create custom_radio_search_location.xml file for custom radio in your drawable

<?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/invite_radio_check" />
        <item android:state_checked="false"
          android:drawable="@drawable/invite_radio_uncheck" />
   </selector>

//than use this custom_radio_search_location.xml in your layout

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="3" >

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:button="@drawable/custom_radio_search_location"
        android:checked="true"
        android:text="A"
        android:textColor="@android:color/black" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:button="@drawable/custom_radio_search_location"
        android:text="B"
        android:textColor="@android:color/black" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:button="@drawable/custom_radio_search_location"
        android:text="C"
        android:textColor="@android:color/black" />
</RadioGroup>
Szczecin answered 2/4, 2015 at 11:57 Comment(0)
E
0

try this one

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:orientation="horizontal"
    android:layout_marginLeft="10dp"
    android:id="@+id/segment_img"

    android:layout_gravity="right|center_vertical"
    android:checkedButton="@+id/button_add">
        <RadioButton
            android:id="@+id/button_male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/violetbg"
            android:button="@drawable/malewhite"  />

        <RadioButton
            android:id="@+id/button_female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:button="@drawable/female"  />       
</RadioGroup>
Eckart answered 2/4, 2015 at 11:31 Comment(3)
I have multiple radio buttons in my XML file. This will be only be in effect with one radio button.Juglandaceous
there are two buttons button_female and button_male.please post your code otherwiseEckart
y did u gave text for radiobutton what do you really want if you give android:background="@color/color" android:button="@drawable/yourpic" you can simply give a picture representation for radio button and can check the onchecked change of radiogroupEckart

© 2022 - 2024 — McMap. All rights reserved.