How to set the background colour of a RadioButton?

android

Solution

You should try this way.. hope this is help you....

<RadioGroup
            android:id="@+id/eligibility"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:orientation="horizontal"
            android:layout_weight=".5" >

            <RadioButton
                android:id="@+id/eligibilityYes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:background="@drawable/checkbox_background"
                android:text="yes" />

            <RadioButton
                android:id="@+id/eligibilityNo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/checkbox_background"
                android:text="No" />
        </RadioGroup>

your checkbox_background.xml file put image in res/drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="false" android:drawable="@drawable/radio_rad" />
    <item android:state_checked="true" android:drawable="@drawable/radio_green" />

</selector>

Problem

Can you please tell me how to set the background colour of a RadioButton? If user selects yes, the background color should change to green. When user selects no, the background color should change to red. here is my xml: ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <include layout="@layout/header" /> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingTop="20dp" android:stretchColumns="2" > <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFF00" > <TextView android:layout_height="wrap_content" android:layout_width="0dp" android:padding="5dp" android:text="Eligibility confirmed:" android:layout_weight=".5"/> <RadioGroup android:id="@+id/eligibility" android:layout_height="wrap_content" android:layout_width="0dp" android:orientation="horizontal" android:layout_weight=".5" > <RadioButton android:id="@+id/eligibilityYes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="yes" /> <RadioButton android:id="@+id/eligibilityNo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="No" /> </RadioGroup> </TableRow> <TableRow android:focusable="true" android:focusableInTouchMode="true" android:background="#FFFF00" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:padding="5dp" android:text="Informed consent given and explained:" android:layout_weight=".5" /> <RadioGroup android:id="@+id/explained" android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight=".5"> <RadioButton android:id="@+id/explainedYes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="yes" /> <RadioButton android:id="@+id/explainedNo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="No" /> </RadioGroup> </TableRow> </TableLayout> <!-- <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="ClickMe"/> --> </LinearLayout> ```

Original source