0

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.

Bansi Doshi
  • 229
  • 1
  • 3
  • 12

1 Answers1

2

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);
Ravi
  • 34,851
  • 21
  • 122
  • 183