Alex Mamo
Alex Mamo

Reputation: 1

How to uncheck all radio buttons in a radio group, when a radio button is checked in Android?

I have created programmatically 5 radio groups with 4 radio buttons each. The problem is, when i first check a radio button from the first radio group and after that i check the second radio button from the same radio group, the first radio button remains checked. What can i do to have the normal behavior for all the radio buttons in the radio group? This is my code:

    radioGroup = new RadioGroup[5];
    answer = new RadioButton[4];
    int i = 0;
    for (Question qn : questions) {
        radioGroup[i] = new RadioGroup(this);
        int j = 0;
        for (Answer an : answers) {
            if (qn.getID() == an.getQuestion_id_answer()) {
                answer[j] = new RadioButton(this);
                answer[j].setText(an.getAnswer());
                radioGroup[i].addView(answer[j]);
                j++;
            }
        }
        linearLayout.addView(radioGroup[i]);
        i++;
    }

Thanks!

Upvotes: 1

Views: 1427

Answers (1)

Andrew V
Andrew V

Reputation: 522

"Checking one RadioButton that belongs to a RadioGroup unchecks any previously checked RadioButton within the same group." Source: developer.android.com

If I understand right: You are talking about 4 RadioButtons in the same RadioGroup that refuse to uncheck themselves when another button in the same RadioGroup gets checked?

If that's the case, just give a different Resource ID to every RadioButton and that should fix it.

Upvotes: 1

Related Questions