163

I have the following code, how do I make it so that the 3 buttons are at the bottom?

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:gravity="center"
        android:text="@string/observer"
        android:textAppearance="?android:attr/textAppearanceLarge"
        tools:context=".asdf"
        android:weight="1" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:text="1" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:text="2" />

        <Button
            android:id="@+id/button3"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:text="3" />
    </LinearLayout>

Ahmad
  • 69,608
  • 17
  • 111
  • 137
thedeepfield
  • 6,138
  • 25
  • 72
  • 107
  • what's this view wrapped in? a frame layout? relative layout? – Nirvana Tikku Feb 08 '13 at 19:17
  • 1
    Your code contains a typo. By `android:weight="1"` you probably meant `android:layout_weight="1"`. This isn't your problem though. – Brian Attwell Feb 08 '13 at 19:32
  • 2
    possible duplicate of [How to align views at the bottom of the screen?](http://stackoverflow.com/questions/2386866/how-to-align-views-at-the-bottom-of-the-screen) – Krupa Patel Jun 11 '14 at 06:24
  • It might be easier to use the space layout found in the toolbox. You can place it on top of the existing layout above the buttons and size it and it will push them to the bottom. – Alex Jan 26 '20 at 01:42
  • You can find a lot of different solutions in this post https://stackoverflow.com/questions/3989883/android-layout-with-listview-between-a-top-bar-and-bottom-bar – Windgate Jul 30 '21 at 13:11

11 Answers11

308

You need to ensure four things:

  • Your outside LinearLayout has layout_height="match_parent"
  • Your inside LinearLayout has layout_weight="1" and layout_height="0dp"
  • Your TextView has layout_weight="0"
  • You've set the gravity properly on your inner LinearLayout: android:gravity="center|bottom"

Notice that fill_parent does not mean "take up all available space". However, if you use layout_height="0dp" with layout_weight="1", then a view will take up all available space (Can't get proper layout with "fill_parent").

Here is some code I quickly wrote up that uses two LinearLayouts in a similar fashion to your code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/db1_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/cow"
        android:layout_weight="0"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="center|bottom"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:text="1" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:text="2" />

        <Button
            android:id="@+id/button3"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:text="3" />
    </LinearLayout>

</LinearLayout>

The result looks like similar to this:

enter image description here

Divins Mathew
  • 2,908
  • 4
  • 22
  • 34
Brian Attwell
  • 9,239
  • 2
  • 31
  • 26
35

You can use a RelativeLayout and align it to the bottom with android:layout_alignParentBottom="true"

AAnkit
  • 27,299
  • 12
  • 60
  • 71
Ahmad
  • 69,608
  • 17
  • 111
  • 137
  • 1
    is it not possible using linear layout? – thedeepfield Feb 08 '13 at 19:16
  • 2
    It is. Like @AND_DEV already said: Use `layout_weight="1"`. With this your LinearLayout will occupy the height left on the screen; now you can set the gravity of your Buttons to bottom. – Ahmad Feb 08 '13 at 19:18
  • 1
    Relativelayouts are hard to read, almost never do what you want and are generally awful to maintain. I'd take the performance hit of 30 linearlayouts over 1 relativelayout any day of the week, simply because it saves hours of frustrating fiddling. – G_V Jun 06 '15 at 07:48
  • @Ahmed i think the question asked is very clear as it is for LinearLayout, But answered for RelativityLayout and Brian Attwell answered clearly with android:gravity="center|bottom" ! – Gopi.cs Jul 10 '19 at 14:33
  • @Gopi.cs I'm really not sure why you're commenting on an answer that I gave more than 6(!) years ago. It's 2019, just use a ConstraintLayout. – Ahmad Jul 10 '19 at 22:13
  • android:layout_alignParentBottom is not available as propertie, is it deprecated? – Windgate Jul 30 '21 at 13:12
  • @Windgate make sure that your parent layout is also a `RelativeLayout` – Ahmad Aug 02 '21 at 01:46
14

Create Relative layout and inside that layout create your button with this line

android:layout_alignParentBottom="true"
varghesekutty
  • 997
  • 2
  • 11
  • 23
6

You can do this by taking a Frame layout as parent Layout and then put linear layout inside it. Here is a example:

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

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:textSize="16sp"/>


<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"      
    android:textSize="16sp"
    />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:textSize="16sp"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
     android:textSize="16sp"/>




</LinearLayout>

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:layout_gravity="bottom"
    />
 </FrameLayout>
Diya Bhat
  • 235
  • 3
  • 12
5

first create file name it as footer.xml put this code inside it.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="78dp"
    android:layout_gravity="bottom"
    android:gravity="bottom"
 android:layout_weight=".15"
    android:orientation="horizontal"
    android:background="@drawable/actionbar_dark_background_tile" >
    <ImageView
        android:id="@+id/lborder"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".14"
        android:background="@drawable/action_bar_left_button"
        android:src="@drawable/overlay" />
    <ImageView
        android:id="@+id/unknown"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".14"
        android:background="@drawable/action_bar_left_button"
        android:src="@drawable/notcolor" />
    <ImageView
        android:id="@+id/open"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".14"
        android:background="@drawable/action_bar_left_button"
        android:src="@drawable/openit"
        />
    <ImageView
        android:id="@+id/color"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".14"
        android:background="@drawable/action_bar_left_button"
        android:src="@drawable/colored" />
        <ImageView
        android:id="@+id/rborder"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/action_bar_left_button"
        android:src="@drawable/frames"
        android:layout_weight=".14" />


</LinearLayout>  

then create header.xml and put this code inside it.:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="@dimen/action_bar_height"
    android:layout_gravity="top"
    android:baselineAligned="true"
    android:orientation="horizontal"
    android:background="@drawable/actionbar_dark_background_tile" >
    <ImageView
        android:id="@+id/contact"
        android:layout_width="37dp"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:layout_weight=".18"
        android:scaleType="fitCenter"
        android:background="@drawable/action_bar_left_button"
        android:src="@drawable/logo"/>

    <ImageView
        android:id="@+id/share"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:layout_weight=".14"
        android:background="@drawable/action_bar_left_button"
        android:src="@drawable/share" />

    <ImageView
        android:id="@+id/save"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".14"
        android:background="@drawable/action_bar_left_button"
        android:src="@drawable/save" />

    <ImageView
        android:id="@+id/set"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".14"
        android:background="@drawable/action_bar_left_button"
        android:src="@drawable/set" />

    <ImageView
        android:id="@+id/fix"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".14"
        android:background="@drawable/action_bar_left_button"
        android:src="@drawable/light" />

    <ImageView
        android:id="@+id/rotate"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".14"
        android:background="@drawable/action_bar_left_button"
        android:src="@drawable/ic_menu_rotate" />

    <ImageView
        android:id="@+id/stock"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".14"
        android:background="@drawable/action_bar_left_button"
        android:src="@drawable/stock" />

</LinearLayout>

and then in your main_activity.xml and put this code inside it :-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity"
android:id="@+id/relt"
android:background="@drawable/background" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="78dp"
    android:id="@+id/down"
    android:layout_alignParentBottom="true" >

    <include
        android:layout_width="fill_parent"
        android:layout_height="78dp"
        layout="@layout/footer" >
    </include>
</LinearLayout>
<ImageView
    android:id="@+id/view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/down"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/inc"
   >  
    </ImageView> 
    <include layout="@layout/header"
        android:id="@+id/inc"
        android:layout_width="fill_parent"
        android:layout_height="50dp"></include> 

happy coding :)

Kosh
  • 6,140
  • 3
  • 36
  • 67
  • Does the order matter? I noticed you put the footer as the first element, and the header as the last. – Martin Konecny Feb 28 '14 at 21:09
  • @MartinKonecny no it does't matter because RelativeLayout has its own attributes so you can control where to other layouts fit inside of it. like : layout_below , etc. – Kosh Mar 01 '14 at 15:54
3
<LinearLayout
 android:id="@+id/LinearLayouts02"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:gravity="bottom|end">

<TextView
android:id="@+id/texts1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="2"
android:text="@string/forgotpass"
android:padding="7dp"
android:gravity="bottom|center_horizontal"
android:paddingLeft="10dp"
android:layout_marginBottom="30dp"
android:bottomLeftRadius="10dp"
android:bottomRightRadius="50dp"
android:fontFamily="sans-serif-condensed"
android:textColor="@color/colorAccent"
android:textStyle="bold"
android:textSize="16sp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>

</LinearLayout>
Syed Danish Haider
  • 1,334
  • 11
  • 15
1

Just add layout_weight="1" to in your linearLayout which having Buttons.

Edit :- let me make it simple

follow something like below, tags name may not be correct, it is just an Idea

<LL>// Top Parrent LinearLayout
   <LL1 height="fill_parent" weight="1" "other tags as requirement"> <TV /><Butons /></LL1> // this layout will fill your screen.
   <LL2 height="wrap_content" weight="1"  orientation="Horizontal" "other tags as requirement"> <BT1 /><BT2/ ></LL2> // this layout gonna take lower part of button height of your screen

<LL/> TOP PARENT CLOSED
AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • adding anroid:layout_weight="1" does not move the linear layout (and buttons) down to the bottom... – thedeepfield Feb 08 '13 at 19:15
  • @AND_DEV, you forgot to set android:gravity. Right now, the buttons will still be at the top, even though the LinearLayout that contains them fills the entire bottom area. – Brian Attwell Feb 08 '13 at 19:38
  • No , Buttons will be in LL2 and that will be in bottom area. Can set Gravity if OP wants Button in exact bottom line. I was just giving a rough Idea, so he can do it according to his requirement. – AAnkit Feb 08 '13 at 20:18
  • Do I read you correctly, that you are using `LL1` element as spacer, so everything next will be at the bottom? Pretty neat trick. – greenoldman Feb 09 '17 at 20:37
1

You can bundle your Button(s) within a RelativeLayout even if your Parent Layout is Linear. Make Sure the outer most parent has android:layout_height attribute set to match_parent. And in that Button tag add 'android:alignParentBottom="True" '

1

You can use the Space widget with layout_weight=1 to fill up the space between your widgets and the bottom buttons. See example:

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

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

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/date_layout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_weight="1"
            android:hint="@string/date_hint">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/date_input"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="false" />

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/time_layout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_weight="1"
            android:hint="@string/time_hint">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/time_input"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:inputType="datetime" />

        </com.google.android.material.textfield.TextInputLayout>

    </LinearLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/food_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:hint="@string/food_hint">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/food_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/calories_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:hint="@string/calories_hint">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/calories_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number" />

    </com.google.android.material.textfield.TextInputLayout>

    <Space
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <Button
        android:id="@+id/add_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/add_button" />

</LinearLayout>
Karl Jamoralin
  • 1,240
  • 1
  • 14
  • 27
0

Add android:windowSoftInputMode="adjustPan" to manifest - to the corresponding activity:

  <activity android:name="MyActivity"
    ...
    android:windowSoftInputMode="adjustPan"
    ...
  </activity>
oskarko
  • 3,382
  • 1
  • 26
  • 26
0

In my case I was trying to put a complete Linear Layout at the bottom in Linear Layout. When I tried using the "layout_gravity" to bottom it was not working. So I changed the root layout to the Frame Layout and it worked perfectly.

So If you want to put a layout at bottom try using Frame Layout and put everything else inside a nested Linear or Relative Layout.

oyeraghib
  • 878
  • 3
  • 8
  • 26