0

I have several radio buttons, each of which has a View.OnClickListener. The whole purpose of those listeners are for me to be able to change the color of the text associated with the RadioButton upon clicking this very RadioButton. My problem is, I'm not sure how to do that.

Here is how I set a listener for each RadioButton:

radioButton.setOnClickListener(badAnswerListener());

Here's the badAnswerListener method:

private View.OnClickListener badAnswerListener(){
     return new View.OnClickListener() {
         @Override
         public void onClick(View arg0) {
             //How do I change the radioButton's color?

         }
     };
}
ci_
  • 8,594
  • 10
  • 39
  • 63
Loïs Talagrand
  • 810
  • 2
  • 13
  • 32

3 Answers3

0

If radioButton is private member of class :

radioButton.setTextColor(R.color.customcolor);

See this too, it's almost the same question :

Changing Text color of RadioButton if clicked in android

Community
  • 1
  • 1
0

if you are only using radio button clicklistners to change the color of text.Have a look at the better approach.To change the text color when radio button is checked(clicked)

1) Use radio_text_selector.xml as below and put it into res/color folder:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_checked="true" android:color="@android:color/red" />
   <item android:color="#504f4f" /> <--default case
</selector>

Use the above selector in "android:textColor" attribute like below

<RadioButton 
     android:id="@+id/radioButton"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"            
     android:textColor="@color/radio_text_selector" />
waleedsarwar86
  • 2,354
  • 1
  • 19
  • 21
0
private View.OnClickListener badAnswerListener(){
     return new View.OnClickListener() {
         @Override
         public void onClick(View arg0) {
             // Change the clicked radioButton's text to red
             ((RadioButton) arg0).setTextColor(Color.RED);
         }
     };
}
Hong Duan
  • 4,234
  • 2
  • 27
  • 50