steve fax
steve fax

Reputation: 71

android first radiobutton remain checked in radiogroup

I'm working on my first Android Studio application and I have a problem with radio buttons that I can't solve out. My application is a sort of multiple choice quiz and when I check the first radio button in the first radio group it remains checked even if I check another radio button of the same radio group, in this case I have two radio buttons checked at the same time as you can see in the picture below.

enter image description here

However, the other radio groups work perfectly. Radio buttons and groups are created programmatically as follows:

 LinearLayout layoutQuiz = findViewById(R.id.layoutQuiz);
 for (int i = 1; i < 5; i++) {
      RadioGroup radioGroup = new RadioGroup(this);
      radioGroup.setId(i);
      TextView text = new TextView(this);
      text.setText(i + ") Question text?");
      layoutQuiz.addView(text);
            
      for (int j = 1; j < 4; j++) {
           RadioButton answer = new RadioButton(this);
           answer.setId(j);
           answer.setText("answer nr" + j);
           radioGroup.addView(answer);
      }
      layoutQuiz.addView(radioGroup);

      radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
        //do something
      });
 }

How could I fix it? Thanks in advance.

Upvotes: 1

Views: 674

Answers (2)

Shay Kin
Shay Kin

Reputation: 2677

I believe there is a conflict between the IDs in the LinearLayout when you use setId() check this answer

I tested your code and it worked with a higher Id for the RadioGroup

 LinearLayout layoutQuiz = findViewById(R.id.layoutQuiz);
        for (int i = 1; i < 5; i++) {
            RadioGroup radioGroup = new RadioGroup(this);
            radioGroup.setId(i*1000); // i make chenge only on this line
            TextView text = new TextView(this);
            text.setText(i + ") Question text?");
            layoutQuiz.addView(text);

            for (int j = 1; j < 4; j++) {
                RadioButton answer = new RadioButton(this);
                answer.setId(j);
                answer.setText("answer nr" + j*i);
                radioGroup.addView(answer);

            }
            layoutQuiz.addView(radioGroup);

            radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
                //do something
            });
        }
            /*
            rest of your code
             */

UPDATE

As @Mnih Ngo Suggests, use View.generateViewId () to avoid ID conflicts when setting it programmatically

radioGroup.setId(View.generateViewId());

Upvotes: 1

Priyanka
Priyanka

Reputation: 247

Hi please check this solution, hope it will work.

LinearLayout layoutQuiz = findViewById(R.id.layoutQuiz);
 int selectedItem;

for (int i = 1; i < 5; i++) {
  RadioGroup radioGroup = new RadioGroup(this);
  radioGroup.setId(i);
  TextView text = new TextView(this);
  text.setText(i + ") Question text?");
  layoutQuiz.addView(text);
        
  for (int j = 1; j < 4; j++) {
       RadioButton answer = new RadioButton(this);
       answer.setId(j);
       answer.setText("answer nr" + j);
      answer.setChecked(i == selectedItem); // Only select button with same index 
       radioGroup.addView(answer);
  }
  layoutQuiz.addView(radioGroup);

  radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
    //do something
    selectedItem=checkedId;//here set the id
  });
}

Upvotes: 0

Related Questions