user1542984
user1542984

Reputation:

How to set the background colour of a RadioButton?

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>

Upvotes: 2

Views: 16546

Answers (3)

Gopal Gopi
Gopal Gopi

Reputation: 11131

try this...

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
            if(radioButton.getText().equals("yes")) {
                radioButton.setBackgroundColor(Color.GREEN);
            } else {
                radioButton.setBackgroundColor(Color.RED);
            }
        }
    });

Upvotes: 3

Bhavesh Vadalia
Bhavesh Vadalia

Reputation: 814

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>

Upvotes: 5

yuva ツ
yuva ツ

Reputation: 3703

android:button parameter of RadioButton. Example-

 <RadioButton
                android:id="@+id/rb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:button="@drawable/radio_button_selector"
                android:textColor="@color/white" />

radio_button_selector.xml-

<?xml version="1.0" encoding="utf-8"?>
<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_dark" android:state_checked="true" />

</selector>

On radiobutton checked listener if radio button is checked you can set radio button.setbackground(..)

Upvotes: 3

Related Questions