148

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?

Lii
  • 11,553
  • 8
  • 64
  • 88
Sandeep Kumar P K
  • 7,412
  • 6
  • 36
  • 40

8 Answers8

258

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

EDIT2:

android:checkedButton="@+id/my_radiobtn"

works in radiogroup xml

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Sprigg
  • 3,340
  • 1
  • 18
  • 26
  • 39
    or if you don't want to id your `RadioButton` and you know the index you can use **((RadioButton)radioGroup.getChildAt(INDEX)).setChecked(true);** – Ahmad Kayyali Apr 29 '13 at 12:32
  • 20
    You shouldn't do that. That will cause the "setChecked" problem described in this question. Of course there are ways to do that without ids of your buttons, but please not by using `setChecked()` One way would be `radiogroup.check(((RadioButton)radioGroup.getChildAt(INDEX)).getId())` or something like that – Sprigg Apr 29 '13 at 13:05
  • 1
    good to not forget to add the radio button to the radio button group _before_ calling group.check(button.getId()) – tom May 02 '16 at 14:23
  • 1
    I had to do it this way: `radioGroup.Check(radioGroup.GetChildAt(0).Id);` for Xamarin.Android users – Mario Galván May 19 '16 at 16:12
117

In case for xml attribute its android:checkedButton which takes the id of the RadioButton to be checked.

<RadioGroup
...
...
android:checkedButton="@+id/IdOfTheRadioButtonInsideThatTobeChecked"
... >....</RadioGroup>
IronBlossom
  • 3,898
  • 3
  • 35
  • 42
60

In the XML file set the android:checkedButton field in your RadioGroup, with the id of your default RadioButton:

<RadioGroup
    ....
    android:checkedButton="@+id/button_1">

    <RadioButton
        android:id="@+id/button_1"
        ...../>

    <RadioButton
        android:id="@+id/button_2"
        ...../>

    <RadioButton
        android:id="@+id/button_3"
        ...../>
</RadioGroup>
sfmirtalebi
  • 370
  • 7
  • 16
Sara
  • 1,844
  • 22
  • 18
20
    RadioGroup radioGroup = new RadioGroup(WvActivity.this);
    RadioButton radioBtn1 = new RadioButton(this);
    RadioButton radioBtn2 = new RadioButton(this);
    RadioButton radioBtn3 = new RadioButton(this);

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


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

    radioGroup.check(radioBtn2.getId());
Ramesh Solanki
  • 2,961
  • 4
  • 28
  • 44
6
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");

radioGroup.addView(radioBtn1);
radioGroup.addView(radioBtn2);
radioGroup.addView(radioBtn3);
radioBtn2.setChecked(true);
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Anuj Oscar
  • 61
  • 1
  • 1
  • The important thing is to use `setChecked()` after all radio buttons have been added to the radio group. Then, `radioGroup.check(radioBtn2.getId())` is not neccessary – Hartmut Pfitzinger Mar 27 '14 at 20:34
3

There was same problem in my Colleague's code. This sounds as your Radio Group is not properly set with your Radio Buttons. This is the reason you can multi-select the radio buttons. I tried many things, finally i did a trick which is wrong actually, but works fine.

for ( int i = 0 ; i < myCount ; i++ )
{
    if ( i != k )
    {
        System.out.println ( "i = " + i );
        radio1[i].setChecked(false);
    }
}

Here I set one for loop, which checks for the available radio buttons and de-selects every one except the new clicked one. try it.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
0

It's a bug of RadioGroup

RadioButton radioBtn2 = new RadioButton(context);

radioBtn2 without viewId, and generateViewId is in onChildViewAdded()

public void onChildViewAdded(View parent, View child) {
    if (parent == RadioGroup.this && child instanceof RadioButton) {
        int id = child.getId();
        // generates an id if it's missing
        if (id == View.NO_ID) {
            id = View.generateViewId();
            child.setId(id);
        }
        ((RadioButton) child).setOnCheckedChangeWidgetListener(
                mChildOnCheckedChangeListener);
    }

    if (mOnHierarchyChangeListener != null) {
        mOnHierarchyChangeListener.onChildViewAdded(parent, child);
    }
}

so, first radioGroup.addView(radioBtn2), then radioBtn2.setChecked(true);

Like this:

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");

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

radioBtn2.setChecked(true);
qinmiao
  • 5,559
  • 5
  • 36
  • 39
0

Add android:checked = "true" in your activity.xml