Java – Uses RadioGroup and RadioButtons

Uses RadioGroup and RadioButtons… here is a solution to the problem.

Uses RadioGroup and RadioButtons

Hey, I want the text of the RadioButton to appear on the left side of the RadioButton. So I tried using 2 relative Views (I have 2 RadioButtons) and it works but the problem is that RadioGroup is not working. Here is the code:

<RelativeLayout android:id="@+id/SearchRelative" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">

<RadioButton android:checked="true" 
        android:id="@+id/SearchRadioButtonAloneWord" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true">
    </RadioButton>

<TextView android:text="1" 
        android:layout_toLeftOf="@+id/SearchRadioButtonAloneWord"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </TextView>
</RelativeLayout>

<RelativeLayout android:id="@+id/SearchRelative" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">

<RadioButton android:id="@+id/SearchRadioButtonInBetweenWord"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true">
    </RadioButton>

<TextView android:text="2"
        android:layout_toLeftOf="@+id/SearchRadioButtonInBetweenWord"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </TextView>
</RelativeLayout>

They display text correctly to the left of their RadioButton. But RadioGroup doesn’t work!
How do I fix this?
Thanks

Edit1: Can I dynamically add a RadioButton to a group? Here’s one way to solve my problem!

Solution

About using the layout manager in RadioGroup: Sorry, it doesn’t work that way. As far as I know, if something is added to a RadioGroup that is not an instance of RadioButton, it is not managed by this RadioGroup. But this provides an alternative solution to the problem: extend the RadioButton to draw it the way you want.

As for dynamic operations: yes, this can be achieved through RadioGroup, just like any other ViewGroup. All you have to do is add a RadioButton via RadioGroup.addView().

Related Problems and Solutions