Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage Layout Inside a Horizontal Oriented Radiogroup

I am using a TableLayout with TableRows as my main activity.

Inside the TableLayout is a Radio Group containing 2 Radio Buttons inside the activity (the Radio Group being inside a table row). I want to be able to align the rightmost radio button to the right edge screen, so the "gap" is between the left radio button and right button (instead of after the right radio button). i.e.

So instead of having
| (x) (x) gap |
I will have
|(x) gap (x)|

where (x) are the Radio Buttons and | are the edges of the screen

I can use gravity (center_horizontal) to put both the buttons in the middle (i.e. | gap (x)(x) gap|) however I can't seem to be able to split them the way I want as said before

like image 587
deteego Avatar asked Dec 29 '10 04:12

deteego


People also ask

How to change orientation of RadioGroup in android?

By Default RadioGroup Will be in Vertical Orientaion , If you want to change use android:orientation="horizontal" attritube of RadioGroup . By Default RadioButton Will be in Left to Right Direction , If you want to change use android:layoutDirection="rtl" attritube of RadioButton .

How do I align a horizontal radio group?

Yes, there is a way. Drag a radio list widget to your screen, go to the Properties tab and select 'Orientation' -> Horizontal.

What is the use of RadioGroup in android studio?

This class is used to create a multiple-exclusion scope for a set of radio buttons. Checking one radio button that belongs to a radio group unchecks any previously checked radio button within the same group. Intially, all of the radio buttons are unchecked.

How to use RadioButton in android?

To create each radio button option, create a RadioButton in your layout. However, because radio buttons are mutually exclusive, you must group them together inside a RadioGroup . By grouping them together, the system ensures that only one radio button can be selected at a time.


1 Answers

All you need to evenly space an arbitrary number of buttons horizontally across the screen:

  1. RadioGroup has to have android:orientation="horizontal" & android:layout_width="fill_parent"
  2. Each radio button has to have android:layout_weight="1", except the rightmost button (to make it line up on the right edge of the screen)!

Landscape screenshot

This took me hours to figure out.

Here is some example code, with a bonus of two text labels and the right and left edges of the screen, for a survey app.

<RadioGroup
    android:id="@+id/radio_group"
    android:orientation="horizontal"
    android:layout_below="@id/question" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone"
>
    <RadioButton
        android:id="@+id/strong_disagree_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="1"
    />
    <RadioButton
        android:id="@+id/disagree_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/disagree"
    />
    <RadioButton
        android:id="@+id/neutral_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/neutral"
    />
    <RadioButton
        android:id="@+id/agree_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/agree"
    />
    <RadioButton
        android:id="@+id/strong_agree_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5"
    />
</RadioGroup>
<TextView
    android:id="@+id/disagree_label"
    android:text="@string/strongly_disagree_txt"
    android:layout_below="@id/radio_group" 
    style="@style/TextAppearance"
    android:visibility="gone"
    />
<TextView
    android:id="@+id/agree_label"
    android:text="@string/strongly_agree_txt"
    android:layout_below="@id/radio_group" 
    android:layout_alignParentRight="true" 
    style="@style/TextAppearance"
    android:layout_width="wrap_content"
    android:visibility="gone"
    />
like image 92
Lucy Avatar answered Oct 16 '22 00:10

Lucy