Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use setTextColor for Android Radio Buttons?

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.

like image 283
Ahmed Avatar asked Mar 29 '12 06:03

Ahmed


People also ask

What are the methods of radio button in Android?

RadioButton is a two states button which is either checked or unchecked. If a single radio button is unchecked, we can click it to make checked radio button. Once a radio button is checked, it cannot be marked as unchecked by user. RadioButton is generally used with RadioGroup.

Which class is used for a set of radio buttons in Android?

A RadioGroup class is used for set of radio buttons.

How do I make sure only one radio button is selected in Android Studio?

You can use android:checkedButton attribute on RadioGroup, providing the id of the RadioButton you want to be checked initially and selecting another RadioButton will clear the previous selection. Save this answer.

How do I change the color of a radio button circle?

Just use the android:buttonTint="@color/colorPrimary" attribute on the <RadioButton> tag. Save this answer.


2 Answers

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));
like image 138
ρяσѕρєя K Avatar answered Oct 25 '22 03:10

ρяσѕρєя K


there are some other ways to do so

myRadioButton.setTextColor(Color.RED);
or
myRadioButton.setTextColor(Color.rgb(red, green, blue)); 
// where red green and blue are the int values

edited if you want to get from resources then use getResources().getColor(R.color.red) ;

like image 26
vipin Avatar answered Oct 25 '22 02:10

vipin