Oreo
Oreo

Reputation: 2594

Align Button to Bottom using LinearLayout

Trying to align button at Bottom using LinearLayout, but getting just below TextView.

To set button at bottom, I am using android:layout_gravity="bottom" but still not done

LinearLayout xml

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="0dp"
        android:layout_height="match_parent"            
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_gravity="bottom"
            android:layout_height="wrap_content">

            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="Bottom" />

        </LinearLayout>

 </LinearLayout>

Upvotes: 2

Views: 24457

Answers (4)

user2984447
user2984447

Reputation: 79

I could not use the suggested answer as I had multiples of 3 vertical ones (each with specific weights) inside a horizontal one. So, ended up using margin top for specific button/widget instead. Working fine so far.

Did not want to change to RelativeLayout as that meant many changes in this scenario.

Example Layout

Upvotes: 0

Ankit Aggarwal
Ankit Aggarwal

Reputation: 5375

Change second linear layout to

<LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal" >

This will put the button at the bottom and this layout will take the rest of the space

Upvotes: 11

Saurabh Vardani
Saurabh Vardani

Reputation: 1861

Yoy have to use like this....

<RelativeLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center">
        <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="Bottom" />
    </RelativeLayout>

Upvotes: 4

tiny sunlight
tiny sunlight

Reputation: 6251

  <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="0dp"
        android:layout_height="match_parent"            
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>

        </LinearLayout>

        <LinearLayout
             android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_gravity="bottom"
            android:layout_height="wrap_content">

            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="Bottom" />

        </LinearLayout>

 </LinearLayout>

Upvotes: 2

Related Questions