ManishPrajapati
ManishPrajapati

Reputation: 514

Changing Text color of RadioButton if clicked in android

I want to change text color of Radio Button if clicked by user. However I can change background color but I don't want that..and can anyone even tell how to put cross or right sign as per right or wrong choice selected...? Here's my code for main class..

final RadioButton Red = (RadioButton)findViewById(R.id.radioButton1);
    //RadioButton Blue = (RadioButton)findViewById(R.id.radioButton2);
    RadioButton Green = (RadioButton)findViewById(R.id.radioButton3);


    Red.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean      isChecked) {
        if(isChecked) {
            Red.setBackgroundColor(Color.RED);
        }

        }
    });
    Green.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub

        }
    });

here's is my layout file:

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

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" 
        />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />
</RadioGroup>

Upvotes: 6

Views: 8914

Answers (2)

Chiamaka Ikeanyi
Chiamaka Ikeanyi

Reputation: 484

    RadioGroup question1RadioGroup = (RadioGroup) findViewById(R.id.radiogroup1);
    question1RadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup view, @IdRes int checkedId) {
            RadioButton Red = (RadioButton) findViewById(R.id.radioButton1);
            RadioButton Green = (RadioButton) findViewById(R.id.radioButton3);

            if (checkedId == R.id.radioButton1) {
                Red.setTextColor(getResources().getColor(R.color.colorRed));
                Red.setBackgroundResource(R.drawable.wrong_option);
            } else if (checkedId == R.id.radioButton3) {
                Green.setTextColor(getResources().getColor(R.color.colorGreen));
                Green.setBackgroundResource(R.drawable.correct_option);
            }
        }
    });

Upvotes: 0

user2742371
user2742371

Reputation:

To change the text color of the radiobutton:

Red.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            Red.setTextColor(Color.RED);
        }
    }
});

For custom icons; create a radiobutton.xml in your drawable folder with this in it:

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

And put these lines in your styles.xml (in your values folder):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CustomTheme" parent="android:Theme">
        <item name="android:radioButtonStyle">@style/RadioButton</item>
    </style>
    <style name="RadioButton" parent="@android:style/Widget.CompoundButton.RadioButton">
        <item name="android:button">@drawable/radiobutton</item>
    </style>
</resources>

Upvotes: 6

Related Questions