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.
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.