Search code examples
androidradio-buttonandroid-radiogroup

How to force RadioGroup to select only one RadioButton at a time programatically?


I am designing customize form programmatically where user can put a question and can add multiple options using radio buttons. I have taken RadioGroup and i am adding radio buttons in it. But while selecting i want only one radio button get selected at a time. How to implement it programmatically. Please help me..

Here is my code

final RadioGroup radioGroup = new RadioGroup(getApplicationContext());
radioGroup.setId(1);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.FILL_PARENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT);

RadioButton radioButtonView = new  RadioButton(getApplicationContext());
radioButtonView.setId(i++);
radioButtonView.setText(addnew);

radioGroup.addView(radioButtonView, p);
loption.addView(radioGroup, p);

Thanks in advance,


Solution

  • It seems that you are making a new RadioGroup for every RadioButton.

    You should add every new RadioButton to the same RadioGroup. The RadioGroup will then make sure only one RadioButton can be selected at the time.