1

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.

steve fax
  • 33
  • 6

2 Answers2

1

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());
Shay Kin
  • 2,539
  • 3
  • 15
  • 22
  • 2
    I believe this answer to be correct. The view-ID needs to be unique for the whole App. My guess is: Using the same ID for multiple RadioButtons made the program behave strangely. I'd suggest generating View-IDs with View.generateViewId() and getting the marked RadioButton through a different mechanism. But the view ID generation and getting-the-marked-button hint are for bigger apps than this exercise. – Minh Ngo Mar 13 '21 at 20:06
  • 1
    Yeah View.generateViewId() is best way but I've never worked with it. 1 question please if we use this method how can I get the id generated to use te View by the following – Shay Kin Mar 13 '21 at 20:13
  • 2
    I left this part out, because your answer gives most of the details and would be a solution. I break the question "looking for the answer through the ID" in 2 parts: 1) the ID needs to be associated with some data (answer and/or question). 2) In most programming languages one can associate a unique ID with with a value by using a map. E.g. a unique viewID with an answer value. Check https://docs.oracle.com/javase/8/docs/api/java/util/Map.html and look for the appropriate Java version, if you're interested. Discuss it with me if you really are interested thru messages. – Minh Ngo Mar 13 '21 at 20:49
  • 1
    thank you very much for the reply. I also found a way to do this with `View.setTag()`; if we want to use the view in the following code by `ViewGroup.findViewWithTag ()` @Minh Ngo – Shay Kin Mar 13 '21 at 21:45
0

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
  });
}
Priyanka
  • 247
  • 4
  • 19