Android Dynamically Generated Radio Buttons Not Unchecking Once Setchecked Programmatically
Solution 1:
After struggling for an hour I've found the solution.
Check the RadioButton after adding it to the RadioGroup.
The solution in Kotlin -
val rb = layoutInflater.inflate(R.layout.custom_radiobutton, null) as RadioButton
rb.text = label
rb.isChecked = false
radioGroup.addView(rb)
if(selected) rb.isChecked = true
Solution 2:
The problem is that you are doing things twice. If you already have a radio group that handles toggling and deselecting others. You dont operate on the radiobuttons individually.
Please read the RadioGroup documentation and implement the proper listeners
RadioButton rd = new RadioButton(this); rd = (RadioButton)rg.getChildAt(i);
This makes no sense, first you create a button and then reassign it to a child of RG.
RadioButtonrd= (RadioButton)rg.getChildAt(i);
Should be the correct form.
you should implement this listener on the radiogroup: http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html
and forget about this part
rd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onRadioButtonClicked(v);
}
});
Solution 3:
The code I added to try to fix this is confusing everyone; sorry.
The original code is:
RelativeLayout rlaAccessPoints;
RadioGroup rg;
privatevoidPopulateAccessPoints(List<clsAccessPoint> accessPoints){
rg = new RadioGroup(this);
for (clsAccessPoint acp : accessPoints) {
RadioButton rd = new RadioButton(this);
rd.setText(acp.ID + ": " + acp.Name);
rg.addView(rd);
if (Settings.AccessPointID.equals(acp.ID)){
rd.setChecked(true);
}
}
rlaAccessPoints.addView(rg);
}
This shows the same incorrect behaviour (the RadioButton that I set programatically not being unset when I click on another in the same RadioGroup).
Please refer to this code instead. This is the only applicable code; there is no custom event handlers etc.
Post a Comment for "Android Dynamically Generated Radio Buttons Not Unchecking Once Setchecked Programmatically"