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,

Martin Pfeffer
  • 12,471
  • 9
  • 59
  • 68
Snehal
  • 233
  • 1
  • 3
  • 13
  • 2
    pls provide some code of what you've tried and what is not working for you – stamanuel Feb 18 '14 at 11:32
  • What is the problem in your implementation right now?if you have tried anything programmatically – Pararth Feb 18 '14 at 11:36
  • So, are you creating a new `RadioGroup` for every `RadioButton`? If all the `RadioButton`s 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? – Andreas Løve Selvik Feb 18 '14 at 11:40
  • *radioButtonView.setId(i++);* where is **`i`** anyway? are you using this whole code inside a for loop? – SMR Feb 18 '14 at 11:40
  • 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. – Snehal Feb 18 '14 at 11:41
  • 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 `RadioButton`s and when you want to add a new `RadioButton`, simply add it to that `RadioGroup` – Andreas Løve Selvik Feb 18 '14 at 11:43
  • 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.. – Snehal Feb 18 '14 at 11:45
  • 1
    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. – Andreas Løve Selvik Feb 18 '14 at 13:16

5 Answers5

3
 @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Log.d(TAG, "onCheckedChanged  " + buttonView.getId());
        mRadioGroup.clearCheck();
        if (isChecked) {
            mRadioGroup.check(buttonView.getId());
        }
    }
Martin Pfeffer
  • 12,471
  • 9
  • 59
  • 68
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.

Andreas Løve Selvik
  • 1,262
  • 16
  • 25
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.

AlexPad
  • 10,364
  • 3
  • 38
  • 48
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;
    }



}
Kishan Dhamat
  • 3,746
  • 2
  • 26
  • 36
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);
Esed
  • 41
  • 2