1

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

Gurcan
  • 428
  • 1
  • 8
  • 19
  • Why not just use ConstraintLayout? – OneCricketeer Sep 10 '17 at 12:53
  • In other words, leaving `android:layout_height` when using weights is usually wrong. Plus you've applied center gravity, so it's not really clear why anything should be at the top or bottom. – OneCricketeer Sep 10 '17 at 12:55
  • 1
    Becuase this is a screen which was created with LinearLayout and I don't want to change all screen. I only want to add a button. (This button should be placed at the bottom) – Gurcan Sep 10 '17 at 13:00
  • I didn't say anything about changing all screens, only this one – OneCricketeer Sep 10 '17 at 13:17

2 Answers2

0

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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Bhuvanesh BS
  • 13,474
  • 12
  • 40
  • 66
0

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>
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245