Android: Horizontal Center Align for Radio Group
Asked Answered
A

4

6

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>
....
Aponte answered 13/5, 2015 at 5:28 Comment(1)
can you post your full layout code,need to know your parent layoutSerica
S
7

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>
Serica answered 13/5, 2015 at 5:46 Comment(0)
B
3
<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>
Burnejones answered 13/5, 2015 at 5:41 Comment(2)
@Aponte here is the solution..it works fine.hope it helps :) and do mark the answer :)Burnejones
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.Aponte
H
1

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

Heartfelt answered 13/5, 2015 at 5:41 Comment(0)
G
1

Try this :

<RadioGroup
                android:id="@+id/account_options_sex"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="16dp"
                android:orientation="horizontal"
                android:scrollbarAlwaysDrawHorizontalTrack="false"
                android:scrollbarAlwaysDrawVerticalTrack="true"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                android:layout_gravity="center_horizontal"
                android:gravity="center_horizontal"
                app:layout_constraintTop_toBottomOf="@+id/account_title_pf_sex">

            <RadioButton
                    android:id="@+id/radio_female"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitStart"
                    android:text="Feminino" />

            <RadioButton
                    android:id="@+id/radio_masculine"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitStart"
                    android:text="Masculino" />

            <RadioButton
                    android:id="@+id/radio_unkown"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitStart"
                    android:text="Indefinido" />
        </RadioGroup>
Guildroy answered 19/9, 2019 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.