3
votes

I can't get the below dynamically generated RadioButtons to uncheck when I click on other RadioButton in the same RadioGroup.

I have even resorted to writing my own handler that clears the RadioGroup (below), and tried another that make all RadioButtons .setChecked(false) but this still does not clear the RadioButton that I setChecked in PopulateAccessPoints.

Any ideas?

RelativeLayout rlaAccessPoints;
RadioGroup rg;

public void onRadioButtonClicked(View view) {
// Is the button now checked?
RadioButton rd = (RadioButton)view;
rg.clearCheck();
rd.setChecked(true);
}


private void PopulateAccessPoints(List<clsAccessPoint> accessPoints){
rg = new RadioGroup(this);

for (clsAccessPoint acp :  accessPoints) {
    RadioButton rd = new RadioButton(this);
    rd.setText(acp.ID + ": " + acp.Name);

    rd.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            onRadioButtonClicked(v);
        }
    });

    rg.addView(rd);
}

rlaAccessPoints.addView(rg);

for (int i = 0; i <= rg.getChildCount() - 1; i++){
    RadioButton rd = new RadioButton(this);
    rd = (RadioButton)rg.getChildAt(i);

    String rdText = rd.getText().toString();
    int colonPos = rdText.indexOf(":");
    rdText = rdText.substring(0, colonPos).toString();
    if (Settings.AccessPointID.equals(rdText)){
        //rg.check(i);
        rd.setChecked(true);
    }
}

}

EDIT: I posted an answer below with much more concise code; please look at that instead.

3
Checkbox's default behaviour is to be checked when user touches it, you dont need to code it programatically . just check if it is checked or not . doing it programatically may result in toggling it twiceP-RAD
Have you tried it without adding an OnClickListener to every button? Supposedly you don't need that, so try without it first.Rickard
I couldn't get this working so changed it to a group of buttons instead.james pearce

3 Answers

2
votes

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
0
votes

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.

RadioButton rd = (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);
    }
});
0
votes

The code I added to try to fix this is confusing everyone; sorry.

The original code is:

RelativeLayout rlaAccessPoints;
RadioGroup rg;

private void PopulateAccessPoints(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.