views:

105

answers:

1

Hi guys,

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.

+1  A: 

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>
smith324
Oops :D Thanks for this. I'll try and get back to you but I think it would work.
hungh3
@hungh3, if this has solved your question I encourage you to accept my answer, this is the StackOverflow way.
smith324
Done! Again, thanks. Btw, do you know how we can get text-button instead of button-text as the normal RadioButton layout?
hungh3
Not that I am familiar with. If the RadioButton class doesn't support it you may just want to use a TextView next to a RadioButton.
smith324