Stamatis Stiliats
Stamatis Stiliats

Reputation: 399

Change the text color of an unidentified radio button

I have a radio group with 4 radio buttons. I use this code :

int radioButtonID = radioGroup.getCheckedRadioButtonId();
View checkedRadioButton = radioGroup.findViewById(radioButtonID);
int idx = radioGroup.indexOfChild(checkedRadioButton);

To retrieve the index of the checked radio button.

The problem is that i want to change the text color of the checked radio button, while i dont know the specific radio button each time. so: checkedRadioButton.setTextColor(color); shows me an error that i need to 'Add a qualifier', which basicaly is showing me that i should use a specific radio button to call that method, like:

radioButtonAns1.setTextColor(color);

I would like if someone would also explain why i have that error and if there is a solution on that. The only method i can call is .setBackgroundColor() which looks ugly.

Thanks in advance!

Upvotes: 0

Views: 458

Answers (3)

Bradley Wilson
Bradley Wilson

Reputation: 1197

An XML alternative would be:

set the TextColor as a @Color selector.

<RadioButton
   android:id="@+id/radio_1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Radio 1"
   android:textColor="@color/radiobuttonstate"/>

The color selector would be:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#000000"/>
    <item android:state_checked="true" android:color="#ffffff"/>
    <item android:color="#00f"/>
</selector>

Upvotes: 0

Remario
Remario

Reputation: 3863

Well it is kinda of straight forward, the error is basically saying hey you trying to change the color of the group containing the groups of radio button.Please choose which one you want first then change the it. To remedy this declare a Boolean array,then set a on click listener to detect the position of the clicked radio button then set then set Boolean value to true in the array.

Upvotes: 0

zsmb13
zsmb13

Reputation: 89658

You can use the setTextColor method if you cast the View that you found to RadioButton:

RadioButton checkedRadioButton = (RadioButton) radioGroup.findViewById(radioButtonID);
checkedRadioButton.setTextColor(color);

Upvotes: 1

Related Questions