Android: Horizontal Center Align for Radio Group

10,843

Solution 1

In radio group layout you mentioned layout width like match_parent,if you mention your parent layout like match parent you cannot use gravity properly.So you need to change width like wrap_content.

<RadioGroup
                android:id="@+id/radioGroupMode"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:layout_gravity="center_horizontal"  
                android:gravity="center_horizontal" >

                <RadioButton
                    android:id="@+id/radioTestMode"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:tag="OPTION_1"
                    android:text="Option1"
                 />

                <RadioButton
                    android:id="@+id/radioLearnMode"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:tag="Option_2"
                    android:text="Option 2"
                 />
            </RadioGroup>

Solution 2

<LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
    android:gravity="center">

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="Test" />


        <RadioGroup
            android:id="@+id/radioGroupMode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_gravity="center_horizontal"  


      android:gravity="center_horizontal">

            <RadioButton
                android:id="@+id/radioTestMode"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:tag="OPTION_1"
                android:text="Option1"
                />

            <RadioButton
                android:id="@+id/radioLearnMode"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:tag="Option_2"
                android:text="Option 2"
                />
        </RadioGroup>

    </LinearLayout>

Solution 3

Problem is with your RadioButton. Kindly change your layout_width to "wrap_content"

Share:
10,843
Jasper
Author by

Jasper

Areas of interest and skill: Java / J2EE / MongoDB / Big Data/ Hadoop / Machine Learning / Cloud / Amazon EC2

Updated on June 10, 2022

Comments

  • Jasper
    Jasper about 2 years

    I have Radio Group which appears left aligned on screen.
    How can i horizontally center the radio group.
    The TextView itself appears properly center-aligned horizontally.

    ....
            <LinearLayout
                android:id="@+id/LinearLayout02"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                >
    
                <TextView
                    android:id="@+id/text1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:text="Test"
                    />
    
    
            <RadioGroup
                android:id="@+id/radioGroupMode"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" 
                android:layout_gravity="center_horizontal"  
                android:gravity="center_horizontal" >
    
                <RadioButton
                    android:id="@+id/radioTestMode"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:tag="OPTION_1"
                    android:text="Option1"
                 />
    
                <RadioButton
                    android:id="@+id/radioLearnMode"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:tag="Option_2"
                    android:text="Option 2"
                 />
            </RadioGroup>
    
    </LinearLayout>
    ....
    
  • ammar shahid
    ammar shahid about 9 years
    @Jasper here is the solution..it works fine.hope it helps :) and do mark the answer :)
  • Jasper
    Jasper about 9 years
    ammar> did not work...but I guess the radio group needs gravity as well, ... your change about wrap_content for radio group part is useful though.