How to use setTextColor for Android Radio Buttons?

Ahmed picture Ahmed · Mar 29, 2012 · Viewed 8.8k times · Source

I am trying to change the text color of a RadioButton (which is defined in an xml layout and is in a RadioGroup) on selecting it.

When I change the text color directly in the Eclipse Android Layout Editor by setting the TextColor property to "@color/red" (which I defined in strings.xml), it works just fine, but when I try to do this programmatically during runtime as

myRadioButton.setTextColor(R.color.red); 

it only turns the color to grey, not to red as intended.

R.color.red (@color/red) is correctly defined as a hex value ("#FF0000"), but it does turn the text color to red in the layout editor, but not via a Java command.

Answer

ρяσѕρєя K picture ρяσѕρєя K · Mar 29, 2012

if your color.xml is like:

<color name="errorColor">#f00</color>

and then use this code to show it:

myRadioButton.setTextColor(getResources().getColor(R.color.red));