Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align radio buttons inside radio group

I'm developing an Android 3.1 Tablet application and asking this here because I haven't found any working answer (or example) in my code.

I have the following piece of code:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1" >

    <LinearLayout
        android:id="@+id/materialsLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
                                                <!-- 0 . Título del fragment -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight=".5"
            android:gravity="center_horizontal"
            android:text="@string/layout_title_quantity"
            android:textAppearance="?android:attr/textAppearanceLarge" />
                                                <!-- 1. Texto -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/layout_qty_size_carton"
            android:textAppearance="?android:attr/textAppearanceMedium" />
                                                <!-- 2. Texto -->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:layout_gravity="center"
            android:orientation="horizontal"
            android:weightSum=".45" >

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight=".1"
                android:gravity="right"
                android:text="@string/layout_req_po"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight=".05"
                android:text="@string/layout_qty_carton"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/qtyPerCartonTxt"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight=".05"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight=".01"
                android:gravity="center"
                android:text="@string/layout_slash"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight=".05"
                android:text="@string/layout_cartoon_size"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/cartonSizeTxt"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight=".05"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight=".01"
                android:gravity="center"
                android:text="@string/layout_slash"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight=".03"
                android:text="@string/layout_weight"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/cartonWeightTxt"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight=".05"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>
                                                <!-- 3. Titulo tabla -->
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/layout_actual_finding"
                android:textAppearance="?android:attr/textAppearanceMedium" />
                                                <!-- 4. Cabecera tabla -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:layout_gravity="center"
            android:orientation="horizontal"
            android:weightSum=".5" >

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight=".07"
                android:gravity="center"
                android:text="@string/layout_cartoon_number"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight=".07"
                android:gravity="center"
                android:text="@string/layout_actual_qty"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight=".07"
                android:gravity="center"
                android:text="@string/layout_actual_size"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight=".07"
                android:gravity="center"
                android:text="@string/layout_actual_weight"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <Button
                android:id="@+id/btnAddNewActPckCtr"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight=".05"
                android:onClick="onAddNewActPckCtrClick"
                android:text="@string/btn_add_new" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:layout_gravity="center"
            android:orientation="horizontal"
            android:weightSum=".5" >

            <TextView
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight=".07"
                android:gravity="right"
                android:text="@string/layout_result"
                android:textAppearance="?android:attr/textAppearanceMedium" />


            <RadioGroup
                android:id="@+id/rGroupResQTYOnline"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_gravity="right"
                android:layout_weight=".07"
                android:orientation="horizontal" >


                <RadioButton
                    android:id="@+id/rResQTYOnlineYes"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="right"
                    android:text="@string/layout_yes" />

                <RadioButton
                    android:id="@+id/rResQTYOnlineNo"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="left"
                    android:text="@string/layout_no" />

            </RadioGroup>

            <RadioGroup
                android:id="@+id/rGroupResSzOnline"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight=".07"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/rResSzOnlineYes"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="@string/layout_yes" />

                <RadioButton
                    android:id="@+id/rResSzOnlineNo"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="@string/layout_no" />
            </RadioGroup>

            <RadioGroup
                android:id="@+id/rGroupResWgOnline"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight=".07"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/rResWgOnlineYes"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="@string/layout_yes" />

                <RadioButton
                    android:id="@+id/rResWgOnlineNo"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="@string/layout_no" />
            </RadioGroup>



        <Button
            android:id="@+id/btnTakeQtyOnlinePhoto"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".07"
            android:onClick="onTakePhotoClick"
            android:text="@string/btn_take_photo" />

        </LinearLayout>


        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dip"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="40dip"
            android:layout_marginTop="40dip"
            android:layout_weight=".1"
            android:gravity="left|center_vertical"
            android:text="@string/layout_photos"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Gallery
            android:id="@+id/quantityOnlineGallery"
            android:layout_width="fill_parent"
            android:layout_height="200dip" />
</LinearLayout>
</ScrollView>

I'm trying to center radio buttons inside radiogroup but I don't know how to do it.

I have tried this code:

 <RadioGroup
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
   <RadioButton
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:layout_gravity="center|left"/>
   <RadioButton
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="center|right" />
    </RadioGroup>

But it doesn't work.

How can I do it?

like image 978
VansFannel Avatar asked Nov 03 '12 08:11

VansFannel


People also ask

Should radio buttons be stacked?

Based on research from the Neilson Norman Group as well as the various human interface guidelines for Apple and Microsoft, radio buttons should have the circle to the left of the label, and the list should be stacked vertically.

How do I get radio buttons side by side on android?

Use the android:orientation="horizontal" -attribute of the RadioGroup.

What property sets the radio button in a group?

Use the GroupName property to specify a grouping of radio buttons to create a mutually exclusive set of controls. You can use the GroupName property when only one selection is possible from a list of available options. When this property is set, only one RadioButton in the specified group can be selected at a time.


1 Answers

It might help to try this: Align your radio group to center

<RadioGroup
                android:id="@+id/rGroupResSzOnline"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight=".07"
                android:gravity="center"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/rResSzOnlineYes"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="Yes" />

                <RadioButton
                    android:id="@+id/rResSzOnlineNo"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="No" />
            </RadioGroup>
like image 94
Kiran Android Avatar answered Oct 19 '22 16:10

Kiran Android