i have created a dynamic radio group with with 3 radio buttons..my problem is that all 3 buttons can be clicked and radio group is not hearing for check changes. I would like the default behavior of radio buttons inside radio group where only one radio button can be checked.
RadioButton[] rb = new RadioButton[question.getAnswers().size()];
RadioGroup rg = new RadioGroup(getActivity());
rg.setOrientation(RadioGroup.VERTICAL);
questionContainer.addView(rg);
for (int i =0; i < question.getAnswers().size(); i++) {
rb[i] = new RadioButton(getActivity());
rb[i].setText(question.getAnswers().get(i));
rg.addView(rb[i]);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
Log.e(debugTag, "radio button position: "+checkedId);
}
});
questionContainer is my fragment's LinearLayout container
the problem actually derived from the library i use to create the radio button. After some digging and 2 very useful links provided by @R. Kirill i do not create a RadioGroup to place the radio buttons inside it. I just handle CompoundButton(RadioButton extend from this class) checked change listener myself.
final RadioButton[] rb = new RadioButton[question.getAnswers().size()];
com.rey.material.widget.CompoundButton.OnCheckedChangeListener c = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked) {
for (RadioButton aRb : rb) {
aRb.setChecked(aRb == compoundButton);
}
}
}
};
for (int i =0; i < question.getAnswers().size(); i++) {
rb[i] = new RadioButton(getActivity());
rb[i].setId(i);
rb[i].setOnCheckedChangeListener(c);
rb[i].setChecked(false);
rb[i].setText(question.getAnswers().get(i));
questionContainer.addView(rb[i]);
}