Paul Hoang
Paul Hoang

Reputation: 1074

Android: How to design to achive the "RadioGroup" effect?

I'm trying to design an Android layout that would look like the following:

Entity1
option1 for Entity1
option2
...
optionN
Entity2
option1 for Entity2
...
optionN
Entity3

...

With the requirement that for each entity, one and only one option is allowed.

And I decided to use radio button with the intention of using RadioGroup to enforce that. I designed as follows to achieve the above:

<table layout>
    <TextView, content = "Entity1"/>
    <TextView, content = "option1 for Entity1"/> <RadioButton/>
    <TextView, content = "option2"> <RadioButton/>
   ...
</table layout>

And the pseudo-code would be:

 RadioGroup raGroup = createEmptyRaGroup()
    ...
    row = new TableRow(); 
    row.add(TextView-entity1);  
    row.add(TextView-op1ForEntity1); 
    RadioButtton raBtn = createRadioButton(); 
    raGroup.addView(raBtn); 
    row.addView(raBtn);  
    ...

And the problem I'm having is that I can't group the radio buttons under one RadioGroup so that only one option is selected. At run-time, Android complains that the radio button must be a direct child of the row. If I add the radio button to the row first, then adding the radio button to the radio group first would then throw a similar run-time error.

Can anyone advise me how to achieve the RadioGroup effect using the above design or otherwise?

Thanks guys.

Upvotes: 0

Views: 1571

Answers (1)

smith324
smith324

Reputation: 13060

Why not just use the RadioGroup inside the layout like following:

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

<RadioButton 
    android:text="Choice 1"
    android:id="@+id/RadioButton01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<RadioButton
    android:text="Choice 2"
    android:id="@+id/RadioButton02" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/>
</RadioGroup>

Upvotes: 2

Related Questions