Stamatis Stiliats Stamatis Stiliats -4 years ago 72
Android Question

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!

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download