0

I have developed a dynamic UI of survey questions, where I have questions answering "YES" or "NO". For questions having answers as Yes/No, I have taken radio-group for user input. How to change the color of the Radio button Highlight color for a specific question , when a specific radioGroup option(Yes/no) is selected everytime? I have Done the following implementation, but it is not working.

 final AppCompatRadioButton[] rb = new AppCompatRadioButton[2];
 final RadioGroup rg = new RadioGroup(context); //create the RadioGroup
 rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
 String[] options = context.getResources().getStringArray(R.array.radio_options_yes_no);

   if (questionList.get(j).getQuestionId().matches("3|16|24"))
   {                    
        rb[0].setHighlightColor(context.getResources().getColor(R.color.red));
        rb[0].setTextColor(context.getResources().getColor(R.color.red));
   }

   //for yes as unsafe option
   else if (questionList.get(j).getQuestionId().matches("21|23|30|32")){
         rb[1].setHighlightColor(context.getResources().getColor(R.color.red));
         rb[1].setTextColor(context.getResources().getColor(R.color.red));
   }
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
AmiteshMG
  • 1
  • 3

1 Answers1

0

add this onclick on all radio button in xml

    android:onClick="onRadioButtonClicked"

now add onRadioButtonCliked method

public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();

// Check which radio button was clicked
switch(view.getId()) {
    case R.id.radio_pirates:
        if (checked)
            // Pirates are the best
        break;
    case R.id.radio_ninjas:
        if (checked)
            // Ninjas rule
        break;
}

}

now apply your logic here and change the color using this code

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

here is the link for further reading
for radio button circle color change

ashad
  • 351
  • 4
  • 13