Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Horizontal Center Align for Radio Group

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>
....
like image 532
Jasper Avatar asked May 13 '15 05:05

Jasper


4 Answers

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>
like image 50
learner Avatar answered Oct 19 '22 02:10

learner


<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>
like image 36
ammar shahid Avatar answered Oct 19 '22 04:10

ammar shahid


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

like image 34
Vinay Jayaram Avatar answered Oct 19 '22 03:10

Vinay Jayaram


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>
like image 45
Carlos Marques Avatar answered Oct 19 '22 03:10

Carlos Marques