Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 217
Snehal Avatar asked Feb 18 '14 11:02

Snehal


2 Answers

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.

like image 31
Andreas Løve Selvik Avatar answered Nov 02 '22 06:11

Andreas Løve Selvik


 @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Log.d(TAG, "onCheckedChanged  " + buttonView.getId());
        mRadioGroup.clearCheck();
        if (isChecked) {
            mRadioGroup.check(buttonView.getId());
        }
    }
like image 184
Martin Pfeffer Avatar answered Nov 02 '22 04:11

Martin Pfeffer