Dynamically added RadioButtons do not listen to onCheckChangedListener()

75 Views Asked by At

I need to add RadioGroups and RadioButtons dynamically and generate desired view and implement onCheckChangedListener() on my dynamically created RadioGroup . The problem is, when a newly added button is checked, it is not unchecked when I am clicking other button. I have already checked this - Issue in radiogroup while adding radiobuttons dynamically , but to no avail.

I am working in a fragment and adding views dynamically inside it. The radiobuttons do not exhibit check changed behavior. Please help me to sort this out.

//optionsGroup(RadioGroup),optionButton(RadioButton) 

    optionsGroup = new RadioGroup(getActivity());
    optionsGroup.setId(radioGroupCount);
    optionsGroup.setPadding(20, 20, 20, 20);
    optionsGroup.setLayoutParams(layparams);
    radioGroupList.add(optionsGroup);
    ArrayList<Options> RadioButtonOptions = question.getQuestionOptions().getOptions();
    for (int i = 0; i < RadioButtonOptions.size(); i++) {
        optionButton = new RadioButton(getActivity());
        optionButton.setId(i);
        optionButton.setText(RadioButtonOptions.get(i).getOptionValue());
        optionsGroup.addView(optionButton);
        optionsGroup.clearCheck();
    }
    optionsGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
          for(int rb=0;rb<optionsGroup.getChildCount();rb++){
              RadioButton btn = (RadioButton) optionsGroup.getChildAt(rb);
              if(checkedId==btn.getId()){
                  radioGroupAnswerList.clear();
                  radioGroupAnswerList.add(btn.getText().toString());
                  return;
              }

          }
        }
    });
    holderlayout.addView(optionsGroup);
2

There are 2 best solutions below

1
Tejas Pandya On

I think problem is you are adding view before assigning its listener . so please try this.

for (int i = 0; i < RadioButtonOptions.size(); i++) {
        optionButton = new RadioButton(getActivity());
        optionButton.setId(i);
        optionButton.setText(RadioButtonOptions.get(i).getOptionValue());


    optionsGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
          for(int rb=0;rb<optionsGroup.getChildCount();rb++){
              RadioButton btn = (RadioButton) optionsGroup.getChildAt(rb);
              if(checkedId==btn.getId()){
                  radioGroupAnswerList.clear();
                  radioGroupAnswerList.add(btn.getText().toString());
                  return;
              }

          }
        }
    });
 optionsGroup.addView(optionButton);
}
4
Muhaiminur Rahman On

You need to uncheck other radiobutton if one radio button clicked right? and all this will be dynamically created radiobutton and radio groupe.. then you have to do this..

//optionsGroup(RadioGroup),optionButton(RadioButton) 

optionsGroup = new RadioGroup(getActivity());
optionsGroup.setId(radioGroupCount);
optionsGroup.setPadding(20, 20, 20, 20);
optionsGroup.setLayoutParams(layparams);
radioGroupList.add(optionsGroup);
ArrayList<Options> RadioButtonOptions = question.getQuestionOptions().getOptions();
for (int i = 0; i < RadioButtonOptions.size(); i++) {
    optionButton = new RadioButton(getActivity());
    optionButton.setId(i);
    optionButton.setText(RadioButtonOptions.get(i).getOptionValue());
    optionsGroup.addView(optionButton);
    optionsGroup.clearCheck();
}
optionsGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
      for(int rb=0;rb<optionsGroup.getChildCount();rb++){
          RadioButton btn = (RadioButton) optionsGroup.getChildAt(rb);
          if(checkedId==btn.getId()){
              radioGroupAnswerList.clear();
              radioGroupAnswerList.add(btn.getText().toString());
              optionsGroup.clearCheck();
               if (isChecked) {
                 optionsGroup.check(btn.getId());
              }
              return;
          }

      }
    }
});
holderlayout.addView(optionsGroup);