0

I'm creating a radio group with radio buttons (from an enumeration) at runtime with the following code.

RadioGroup radioGroup = new RadioGroup(this);

List<LocationTypeEnum> warningTypes = preferences.getWarningTypes();
for (LocationTypeEnum enumElement : warningTypes) {
    RadioButton radio = new RadioButton(this);
    radio.setText(enumElement.toString());

    //Check one specific radio by default
        radio.setChecked(enumElement.intValue == userDefinedLocation.getType().intValue);

    radioGroup.addView(radio, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}

When it comes to screen and I try to change the radio, both options remain checked:

enter image description here

What's going wrong?

Cléssio Mendes
  • 996
  • 1
  • 9
  • 25
  • As i was telling in my comment, the id might be the issue. Follow this [link](http://stackoverflow.com/questions/8265034/android-radiogroup-checks-more-than-one-radiobutton). Just add `radio.setId(i++);` in your code. – cosmincalistru Jan 15 '13 at 20:31

2 Answers2

3

Found the answer. You must add the RadioButton to the RadioGroup before setting it to checked, or else the radio group gets lost. Following, the correct code.

    RadioGroup radioGroup = new RadioGroup(this);

    List<LocationTypeEnum> warningTypes = preferences.getWarningTypes();
    for (LocationTypeEnum enumElement : warningTypes) {
        RadioButton radio = new RadioButton(this);
        radio.setText(enumElement.toString());

        //First, add the radio to the group
        radioGroup.addView(radio, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        //Only after that you can check it.
        radio.setChecked(enumElement.intValue == userDefinedLocation.getType().intValue);

    } 

Sounds like a bug, to me. Credits to http://code.google.com/p/android/issues/detail?id=1772

Cléssio Mendes
  • 996
  • 1
  • 9
  • 25
0

Don't know how to comment to ask you to post your layout .xml file. Make sure that you also group them in your xml file like below:

<RadioGroup
        android:id="@+id/radioSex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radioMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_male" 
            android:checked="true" />

        <RadioButton
            android:id="@+id/radioFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_female" />

    </RadioGroup>
user1202888
  • 1,043
  • 2
  • 11
  • 19
  • Thanks. But the layout .xml is quite simple. The radio group itself is completely created at runtime because it is obtained from an enumeration list of values converted to radio buttons as options. (ps: you can only comment others posts after reaching 50 reputation) – Cléssio Mendes Jan 15 '13 at 20:17
  • 1
    As it happened to me, this example works ok, but if you leave the buttons in the same `RadioGroup` but you do not assign id for each of them (radioMale and radioFemale in this case) the issue in question appears. Maybe that could help. – cosmincalistru Jan 15 '13 at 20:26