In Android, OnCheckedChanged Of A Radiogroup Is Not Triggered When I Programmatically Set A Radiobutton By Call SetChecked(true)
Solution 1:
To fire a radio check box for the default when initializing. Set everything to unchecked with the clearCheck method. Then, set the handler and finally set the default and your handler will fire.
itemtypeGroup.clearCheck();
then, same as usual add a listener:
mRadioGroup = (RadioGroup)findViewById(R.id.footer_radio);
mRadioGroup.setOnCheckedChangeListener(this);
mRadioGroup.clearCheck();
RadioButton btn = (RadioButton)view.findViewById(R.id.footer_btn_course);
btn.setChecked(true);
Solution 2:
I am not sure for the issue, but it may be you miss refer your radio group id.
Try this instead to see the effect (inside your onCreate()
):
RadioGroup rdGroup = (RadioGroup) findViewById(R.id.footer_radio);
rdGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
RadioButton btn = (RadioButton)findViewById(checkedId);
if(btn != null){
do_something();
}
}
});
Or may be (RadioButton)findViewById(checkedId)
return null.
Try Spinner
if your case gets ambiguous.
Solution 3:
Instead of btn.setChecked(true)
;
Used btn.performClick();
this will work.
Solution 4:
I had the same problem..
and here is my approach:
class Myclass extends Activity{
protected void onCreate(Bundle savedInstanceState) {
radiogroup = (RadioGroup) findViewById(R.id.radiogroupDecisionUncertain);
radio1 = (RadioButton) findViewById(R.id.radiobuttonNo_U);
radio2 = (RadioButton) findViewById(R.id.radiobuttonYes_U);
// declare the onCheckChangedListener
radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (checkedId == radio1.getId()) { //do something
} else { // do other thing
}
}
});
radiogroup.check(radio1.getId());
}
}
Notice that i set the checked radio button after registering the listener..
Solution 5:
Implement OnCheckedChangeListener
instead of RadioGroup.onCheckedChangeListene
r that will work.
public class MainActivity extends ActivityGroup implements OnCheckedChangeListener{
.....
}
Post a Comment for "In Android, OnCheckedChanged Of A Radiogroup Is Not Triggered When I Programmatically Set A Radiobutton By Call SetChecked(true)"