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

14,862

Solution 1

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.

Solution 2

 @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Log.d(TAG, "onCheckedChanged  " + buttonView.getId());
        mRadioGroup.clearCheck();
        if (isChecked) {
            mRadioGroup.check(buttonView.getId());
        }
    }

Solution 3

I have tried the same problem when I insert LinearLayout to put the radiobuttons in a grid.

I solved this way. Considering that we have more RadioGroups, we know the RadioButton that was pressed. For this reason, just remove the listener from all of your RadioGroups and then deselect all the RadioButtons, resetting onCheckedChangeListener to your RadioGroup.

This is my code:

onCheckedChangeListener= new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
            clearAllRadioButton();
            ((RadioButton) binding.getRoot().findViewById(i)).setChecked(true);
            resetListenerRadioGroup();
        }
    };

    onCheckedChangeListener2 = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
            clearAllRadioButton();
            ((RadioButton) binding.getRoot().findViewById(i)).setChecked(true);
            resetListenerRadioGroup();
        }
    };


    binding.rg.setOnCheckedChangeListener(onCheckedChangeListener);
    binding.rg2.setOnCheckedChangeListener(onCheckedChangeListener2);



private void clearAllRadioButton() {
    binding.rg2.setOnCheckedChangeListener(null);
    binding.rg.setOnCheckedChangeListener(null);
    binding.failed.setChecked(false);
    binding.draft.setChecked(false);
    binding.sent.setChecked(false);
    binding.inbox.setChecked(false);
}

private void resetListenerRadioGroup(){
    binding.rg2.setOnCheckedChangeListener(onCheckedChangeListener2);
    binding.rg.setOnCheckedChangeListener(onCheckedChangeListener);
}

*do not use the radioButton's clearCheck () because it does not respond well when resetting listeners.

Share:
14,862
Snehal
Author by

Snehal

Updated on June 07, 2022

Comments

  • Snehal
    Snehal about 2 years

    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,