Bansi Doshi
Bansi Doshi

Reputation: 229

Multiple radiobutton selecion issue- dynamic radiobutton creation

I am getting issue with radiobutton. I have created dynamic radiobuttons but i am able to multiple select.I need to check only one as per radiobutton functionality. Here is my code:

 List<String> answers = Constants_Variables.missionMainResponse.getResponse().getData().get(early_pos).getQuestions().get(position).getANS();
        ViewGroup answer_radio_group = (ViewGroup) itemView.findViewById(R.id.answer_radio_group);

        LinearLayout ll = new LinearLayout(context);
        ll.setOrientation(LinearLayout.VERTICAL);
        final RadioButton[] rb = new RadioButton[answers.size()];
        Log.v("", TAG + "=Answers list size=" + answers.size());
        for (int j = 0; j < answers.size(); j++) {
            Log.v("", TAG + "=Answers=" + j);
            rb[j] = new RadioButton(context);
            rb[j].setText(answers.get(j) + "");
            rb[j].setId(j);
            ll.addView(rb[j]);
        }
        rb[0].setChecked(true);
        answer_radio_group.addView(ll);

Please help me out I want to select only one option. Thanks.

Upvotes: 0

Views: 93

Answers (1)

Ravi
Ravi

Reputation: 35569

you need to add RadioButtons in RadioGroup first and than in other view or layout.

answer_radio_group.addView(ll); this line should be inside for loop

for (int j = 0; j < answers.size(); j++) {
        Log.v("", TAG + "=Answers=" + j);
        rb[j] = new RadioButton(context);
        rb[j].setText(answers.get(j) + "");
        rb[j].setId(j);
        answer_radio_group.addView(rb[j]);

    }
    rb[0].setChecked(true);

Upvotes: 2

Related Questions