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

5

8

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,

Stenography answered 18/2, 2014 at 11:31 Comment(8)
pls provide some code of what you've tried and what is not working for youStrawboard
What is the problem in your implementation right now?if you have tried anything programmaticallyTannenbaum
So, are you creating a new RadioGroup for every RadioButton? If all the RadioButtons go into the same RadioGroup, you should get the behaviour you are looking for? Or are the code snippet from multiple places in your code?Macmacabre
radioButtonView.setId(i++); where is i anyway? are you using this whole code inside a for loop?Jiffy
I have added radio buttons in radio group. Right now i can select multiple radio buttons. But i want only one radio button to be selected at at time.Stenography
Could you show us the surrounding code as well? I think what you need to do is to keep a reference to the RadioGroup of all the other RadioButtons and when you want to add a new RadioButton, simply add it to that RadioGroupMacmacabre
Thanks... i got my error..by mistake i am creating radiogroup everytime..that's why its not working properly..Anyways Thnks..I will try it out..Stenography
No problem :) It would be nice if you could mark my answer as the solution if it did indeed work. The same goes for all the questions you have asked earlier.Macmacabre
W
2

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.

Wisconsin answered 18/2, 2014 at 11:49 Comment(0)
R
3
 @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Log.d(TAG, "onCheckedChanged  " + buttonView.getId());
        mRadioGroup.clearCheck();
        if (isChecked) {
            mRadioGroup.check(buttonView.getId());
        }
    }
Rhinoscopy answered 30/10, 2015 at 8:3 Comment(0)
W
2

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.

Wisconsin answered 18/2, 2014 at 11:49 Comment(0)
P
1

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.

Phenomenology answered 23/9, 2017 at 14:30 Comment(0)
S
0

Check this out it will work for you:

First Get RadioButton:

RadioButton radioBalls = (RadioButton) findViewById(R.id.radioGameInPlayBalls);
RadioButton radioTime = (RadioButton) findViewById(R.id.radioGameInPlayTime);
radioBalls.setOnCheckedChangeListener(this);
radioTime.setOnCheckedChangeListener(this);

Now in @overide method:

 @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean c) {
        switch (buttonView.getId()) {
        case R.id.radioGameInPlayBalls:
            if (radioTime.isChecked() && c) {
                radioBalls.setChecked(false);
                return;
            }
            radioBalls.setEnabled(c);
        break;
    case R.id.radioGameInPlayTime:
        if (radioBalls.isChecked() && c) {
            radioTime.setChecked(false);
            return;
        }
        radioTime.setEnabled(c);
        break;
    default:
        break;
    }



}
Sturgis answered 18/2, 2014 at 11:47 Comment(0)
B
0

You should check a radio button using RadioGroup, not radio button directly. Below, I populate radio group from list of options and get the radio button id of option to be checked and check the corresponding button after populating radio group. Same goes if you determine the checked option by index.

int checkedId = -1; 

for(JsonElement option : options){

    RadioButton radioButton = new RadioButton(getContext());

    radioGroup.addView(radioButton);

    if(checkedOptionId.equals(id)){
        checkedId = radioButton.getId();
    }
}

radioGroup.check(checkedId);
Broderic answered 14/7, 2016 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.