Android layout_gravity Bottom/Top in LinearLayout

4.1k Views Asked by At

There are millions of questions and answers in related to "layout_gravity Bottom/Top in LinearLayout" on stack overflow but I still haven't solved my problem.

I want to place my EditText which says "Please place me at the very top" at the very top, and TextView which says "Please place me at very bottom" at the very bottom. My dream is very basic but I cannot achieve it!!

Can anyone help me?

This is my XML:

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


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="210dp"
        android:background="@android:color/holo_green_light"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:weightSum="1">

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="Please place me at very top" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@android:color/background_light"
            android:text="TextView"
            android:layout_weight="0.19" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="248dp"
        android:layout_weight="0.70"
        android:background="@color/colorAccent"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:weightSum="1">

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="62dp"
            android:layout_gravity="top"
            android:text="Button" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.16"
            android:background="@android:color/background_light"
            android:text="Please place me at very bottom" />

    </LinearLayout>

</LinearLayout>

And this is my output:

enter image description here

2

There are 2 best solutions below

1
On

You have set gravity bottom for your second linear layout to place the views at the bottom.

change this line at first LinearLayout:

android:gravity="center_vertical"

To:

android:gravity="top"

change this line at Second LinearLayout:

android:gravity="center_vertical"

To:

android:gravity="bottom"

Note: Avoid using nested weights it will slow down the performance.

0
On

If LinearLayout isn't working, then don't force yourself to use it.

  1. Nested LinearLayouts and weights are bad for performance.

  2. RelativeLayout (or ConstraintLayout) are naturally meant to place things at the anchor points of ViewGroups

I assume you want the text and button centered based on your other attributes

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


    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@android:color/holo_green_light"
            android:layout_weight="1">

        <EditText
                android:id="@+id/editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:layout_alignParentTop="true"
                android:inputType="textPersonName"
                android:text="Please place me at very top" />

        <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:background="@android:color/background_light"
                android:text="TextView" />

    </RelativeLayout>

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@android:color/holo_red_light"
            android:orientation="vertical">

        <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="62dp"
                android:layout_centerInParent="true"
                android:text="Button"/>

        <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:background="@android:color/background_light"
                android:text="Please place me at very bottom"/>

    </RelativeLayout>

</LinearLayout>

Alternatively, you only need android:gravity="bottom" on the second LinearLayout, then all views within are at the bottom.

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

    <!-- Top Gravity -->
    <LinearLayout
            android:gravity="top"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@android:color/holo_green_light"
            android:orientation="vertical"
            android:layout_weight="0.5">

        <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Please place me at very top"/>

        <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/background_light"
                android:text="TextView"/>

    </LinearLayout>

    <!-- Bottom Gravity -->
    <LinearLayout
            android:gravity="bottom"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.50"
            android:background="@android:color/holo_red_light"
            android:orientation="vertical">

        <Button
                android:layout_width="match_parent"
                android:layout_height="62dp"
                android:text="Button"/>

        <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/background_light"
                android:text="Please place me at very bottom"/>

    </LinearLayout>

</LinearLayout>