my radio buttons work fine, except that the second one that is chosen becomes sticky. others can be chosen and they toggle, but the display shows the second button stuck on.
please see self contained code example below. i am using 4.0.3 and running in the emulator using eclipse.
thanks
package small.example;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
public class SmallActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout main=new LinearLayout(this);
main.setOrientation(LinearLayout.VERTICAL);
int question=1;
button=new Button(this);
button.setText("Make a choice");
main.addView(button);
button.setId(question);
setContentView(main);
int answers=4;
radioButtons=new RadioButton[answers];
RadioGroup radioGroup=addRadioButtonsToGroup(question,radioButtons);
LinearLayout linearLayout=new LinearLayout(this);
linearLayout.addView(radioGroup);
linearLayout.setId(question);
// radioButtons[0].setChecked(true);
main.addView(linearLayout);
}
private RadioGroup addRadioButtonsToGroup(int question,RadioButton[] radioButtons) {
RadioGroup radioGroup=new RadioGroup(this);
for(int i=0;i<radioButtons.length;i++) {
radioButtons[i]=new RadioButton(this);
radioButtons[i].setText("Question "+question+" Choice "+(i+1));
radioButtons[i].setId(i);
radioGroup.addView(radioButtons[i]);
}
radioGroup.setOnCheckedChangeListener(radioGroupOnCheckedChangeListener);
radioGroup.setId(question);
return radioGroup;
}
RadioButton[] radioButtons;
Button button;
RadioGroup.OnCheckedChangeListener radioGroupOnCheckedChangeListener=new RadioGroup.OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group,int checkedId) {
int id=checkedId;
button.setText(radioButtons[id].getText());
}
};
}
It seems to be that the offending button shares an id with the radioGroup’s id, caused by the line
Why this should cause the button to be sticky isn’t clear to me. If you change that line to
thereby giving it a non-clashing id, all buttons work OK.