Mindaugas Bernatavičius
Mindaugas Bernatavičius

Reputation: 3909

Buttons ar not gravitating towards center of a linear layout

I have this layout.xml, I change the "layout_gravity" atribute for the buttons but no matter what value do I assign both buttons are placed to the left of the linearlayout container. I want them to be center-justified (the comments are for pedagogic reasons, but I placed them just in case they might be the problem) :

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
xmlns:android="http://schemas.android.com/apk/res/android">
<!--root element must specify the Android resource XML namespace -->

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="24dp"
    android:text="@string/question_text"/>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/true_button" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/false_button"
        android:layout_gravity="right" />
    <!--The XML elements :: <string name="false_button">False</string>
        will have to be defined in strings.xml-->

</LinearLayout>

Upvotes: 0

Views: 80

Answers (3)

Chintan Rathod
Chintan Rathod

Reputation: 26034

Try following layout. I edited android:gravity="center_horizontal" to second LinearLayout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <!-- root element must specify the Android resource XML namespace -->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:text="Question" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="True" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="False" />
    </LinearLayout>

</LinearLayout>

Explanation

First LinearLayout has gravity=center. Right. It will center its children. Absolutely Right. But second LinearLayout is also centered but its again a holder. So it has its own properties. Though second layout's parent layout has gravity=center, it will be only applied to its immediate child. Not child's child. So, you have to again declare any other attributes once again to apply to its children.

Upvotes: 1

Hamid Shatu
Hamid Shatu

Reputation: 9700

You can manage this using android:layout_weight as follows...

<!-- root element must specify the Android resource XML namespace -->

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="24dp"
    android:text="@string/question_text" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2" >

    <Button
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/true_button" />

    <Button
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="1"
        android:text="@string/false_button" />
    <!--
    The XML elements :: <string name="false_button">False</string>
    will have to be defined in strings.xml
    -->

</LinearLayout>

Upvotes: 0

Piotr Ślesarew
Piotr Ślesarew

Reputation: 2836

Just change android:layout_width="match_parent" to android:layout_width="wrap_content" in your internal LinearLayout.

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:text="@string/question_text"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/false_button" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/true_button"
            android:layout_gravity="right" />

    </LinearLayout>
</LinearLayout>

Upvotes: 1

Related Questions