Sandy Sandy - 5 months ago 31
Android Question

how to set radio button checked as default in radiogroup with Android

I have created

RadioGroup
and
RadioButton
dynamically as following

RadioGroup radioGroup = new RadioGroup(context);
RadioButton radioBtn1 = new RadioButton(context);
RadioButton radioBtn2 = new RadioButton(context);
RadioButton radioBtn3 = new RadioButton(context);

radioBtn1.setText("Less");
radioBtn2.setText("Normal");
radioBtn3.setText("More");

radioBtn2.setChecked(true);

radioGroup.addView(radioBtn1);
radioGroup.addView(radioBtn2);
radioGroup.addView(radioBtn3);


Here step
radioBtn2.setChecked(true);
causes
radioBtn2
always checked. That means I cannot uncheck
radioBtn2
by checking other two radio buttons (
radioBtn1
,
radioBtn3
). I want to make that
RadioGroup
can check only one radio button at a time (Now it can check two radiobutton at a time). How can I solve this problem?

Answer

you should check the radiobutton in the radiogroup like this:

radiogroup.check(IdOfYourButton)

Of course you first have to set an Id to your radiobuttons

edit: i forgot, radioButton.getId() works as well, thx Ramesh

Comments