You are on page 1of 16

12/12/2018 android - How to align views at the bottom of the screen?

- Stack Overflow

How to align views at the bottom of the screen? Ask Question

Here's my layout code;

<?xml version="1.0" encoding="utf-8"?>


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

<TextView android:text="@string/welcome"
android:id="@+id/TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>

<LinearLayout android:id="@+id/LinearLayout"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom">

<EditText android:id="@+id/EditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>

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

</LinearLayout>

</LinearLayout>

What this looks like is on the left and


what I want it to look like is on the
right.

The obvious answer is to set the


TextView to fill_parent on height but
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our
this causes no room to be left for the
Terms of Service.
button or entry field. Essentially the
https://stackoverflow.com/questions/2386866/how-to-align-views-at-the-bottom-of-the-screen 1/16
12/12/2018 android - How to align views at the bottom of the screen? - Stack Overflow

issue is that I want the submit button


and the text entry to be a fixed height
at the bottom and the text view to fill
the rest of the space, similarly in the
horizontal Linear layout I want the
submit button to wrap its content and
for the text entry to fill the rest of the
space.

If the first item in a Linear Layout is


told to fill_parent it does exactly that,
leaving no room for other items, how
do I get an item which is first in a
linear layout to fill all space apart from
the minimum required by the rest of
the items in the layout?

EDIT:

Relative Layouts were indeed the


answer - Thank you!

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
android:text="@string/welcome"
android:id="@+id/TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
</TextView>

<RelativeLayout
android:id="@+id/InnerRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >

<Button
android:text="@string/label_submit_button"
android:id="@+id/Button"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>

<EditText
android:id="@+id/EditText"
android:layout_width="fill_parent"
android:layout_toLeftOf="@id/Button"
android:layout_height="wrap_content">
</EditText>

</RelativeLayout>

</RelativeLayout>

android xml user-interface

android-layout

edited Feb 8 '17 at 14:22


Community ♦
By using our site, you acknowledge that you have read and understand our , , and our
1 1
.

https://stackoverflow.com/questions/2386866/how-to-align-views-at-the-bottom-of-the-screen 2/16
12/12/2018 android - How to align views at the bottom of the screen? - Stack Overflow
asked Mar 5 '10 at 13:04
gav
15.7k 21 54 87

5 How did you create those


screenshots? – oneself Oct 16 '10 at
19:16

7 @oneself Paint ;) I'm using a skin on


the emulator though courtesy of Tea
Vui Huang teavuihuang.com/android
– gav Apr 24 '11 at 10:20

14 +1 for posting the layout XML that


solved it. Helped me figure out what
was wrong with mine. – Howler Feb
29 '12 at 19:23

the edit provides the answer. Very


perfect – Ghost Worker Apr 12 at
16:30

18 Answers

Não encontrou uma ✕


resposta? Pergunte em
Stack Overflow em
Português.

The modern way to do this is to have


a ConstraintLayout and constrain the
bottom of the view to the bottom of
the ConstraintLayout with
app:layout_constraintBottom_toBottomO
f="parent"

The example below creates a


FloatingActionButton that will be
aligned to the end and the bottom of
the screen.

<android.support.constraint.Constraint
xmlns:android="http://schemas.andro
xmlns:app="http://schemas.android.c
xmlns:tools="http://schemas.android
android:layout_height="match_parent
android:layout_width="match_parent"

<android.support.design.widget.Floatin
android:layout_height="wrap_conten
android:layout_width="wrap_content

app:layout_constraintBottom_toBott

app:layout_constraintEnd_toEndOf="

</android.support.constraint.Constrain

For reference I will keep my old


answer.
Before the introduction of
By using our ConstraintLayout
site, you acknowledge
thethat you have
answer wasread
a and understand our , , and our
relative
. layout.

https://stackoverflow.com/questions/2386866/how-to-align-views-at-the-bottom-of-the-screen 3/16
12/12/2018 android - How to align views at the bottom of the screen? - Stack Overflow

If you have a relative layout that fills


the whole screen you should be able
to use
android:layout_alignParentBottom to
move the button to the bottom of the
screen.

If your views at the bottom are not


shown in a relative layout then maybe
the layout above it takes all the
space. In this case you can put the
view that should be at the bottom,
first in your layout file and position the
rest of the layout above the views
with android:layout_above . This
enable the bottom view to take as
much space as it needs and the rest
of the layout can fill all the rest of the
screen.

edited Nov 19 at 15:26

answered Mar 5 '10 at 13:14


Janusz
105k 101 276 355

4 FYI: that wouldn't work inside a


ScrollView. This is the issue I am
facing now. See
stackoverflow.com/questions/3126347
/… – Igor Ganapolsky May 23 '12 at
0:40

6 That is correct ScrollViews and


Relative Layouts do not mix very well.
If you have to much content to display
everything on one page just use a
linearlayout and put in the bottom view
last. If you want the view to appear
last in the scrollview on small screens
and at the bottom of the page on
bigger phones consider using different
layout files and uses ressource

qualifiers to let the device choose the


correct layout file. – Janusz May 23
'12 at 7:46

ok.. what's the point of the second


paragraph of the answer here? "You
should find a layout which covers the
bottom of the screen first"? then we
should use layout_alignParentBottom
inside the layout? What do we need
android:layout_above for? –
eugene Jul 7 '13 at 12:52

You need the above if you want to


something like a bar at the bottom and
then a LinearLayout above this bar
that stretches from the top of the
screen to the bar. – Janusz Oct 18 '14
at 10:45
By using our site, you acknowledge that you have read and understand our , , and our
1 I have read in one of "Android
.
Weekly" that RelativeLayout is
memory consuming and should be
https://stackoverflow.com/questions/2386866/how-to-align-views-at-the-bottom-of-the-screen 4/16
12/12/2018 android - How to align views at the bottom of the screen? - Stack Overflow
memory consuming and should be
avoided whenever it is possible. –
Tomasz Mularczyk Jun 27 '15 at
19:11

In a ScrollView this doesn't work, as


the RelativeLayout would then
overlap whatever is in the ScrollView
at the bottom of the page.

I fixed it using a dynamically


stretching FrameLayout :

<ScrollView
xmlns:android="http://schemas.andr
android:layout_height="match_paren
android:layout_width="match_parent
android:fillViewport="true">
<LinearLayout
android:id="@+id/LinearLayout0
android:layout_width="match_pa
android:layout_height="match_p
xmlns:android="http://schemas.
android:orientation="vertical"

<!-- content goes here

<!-- stretching frame


<FrameLayout
android:layout_width="matc
android:layout_height="0dp
android:layout_weight="1">
</FrameLayout>

<!-- content fixated t


<LinearLayout
android:layout_width="matc
android:layout_height="wra
android:orientation="horiz
<!-
</LinearLayout>
</LinearLayout>
</ScrollView>

edited Mar 1 '16 at 19:11


Mykola
2,839 6 14 37

answered Nov 4 '10 at 16:50


Bram Avontuur
1,441 1 9 2

1 this is great - also, it's "minimal


invasive", and more intuitive than the
RelativeLayout approach. I'd give

more than one upvote if I could! –


manmal Aug 30 '11 at 12:31
By using our site, you acknowledge that you have read and understand our , , and our
4 I use your solution in my application,
.
but in my case I want that when
keyboard appears the buttons stay at
https://stackoverflow.com/questions/2386866/how-to-align-views-at-the-bottom-of-the-screen 5/16
12/12/2018 android - How to align views at the bottom of the screen? - Stack Overflow
keyboard appears, the buttons stay at
the bottom of the screen (behind the
keyboard) There is a sulution for that
? Thaks. – Yoann Jan 19 '12 at 16:02

1 @DoubleYo: in the manifest


android:windowSoftInputMode="adjust
Pan" – Kopfgeldjaeger Jul 13 '12 at
12:56

2 IF you want the bottom to be fixed


permanently so that is shows all the
time this does not work. Otherwise it
works. – Karl Morrison Nov 2 '12 at
17:06

Nice trick. Works well. – Makalele Jul


27 at 8:56

You can keep your initial linear layout


by nesting the relative layout within
the linear layout:

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent

<TextView android:text="welcome"
android:id="@+id/TextView"
android:layout_width="fill_par
android:layout_height="wrap_co
</TextView>

<RelativeLayout
android:layout_width="match_pa
android:layout_height="match_p
<Button android:text="submit"
android:id="@+id/Button"
android:layout_width="wrap
android:layout_height="wra
android:layout_alignParent
android:layout_alignParent
</Button>
<EditText android:id="@+id/Edi
android:layout_width="matc
android:layout_height="wra
android:layout_toLeftOf="@
android:layout_alignParent
</EditText>
</RelativeLayout>
</LinearLayout>

edited Jun 25 '11 at 21:05

answered Apr 24 '11 at 0:53


pseudosudo
1,382 1 14 26

2 This is a messy approach in that it's


not very modular. You shouldn't have
overlapping layouts. This design won't
work as soon as you want to change
the background of the RelativeLayout
By using our site,oryou acknowledge
want to make anythat youviews
of the have read and understand our , , and our
.
bigger or add views. – Patrick Jun 18
'15 1 21
https://stackoverflow.com/questions/2386866/how-to-align-views-at-the-bottom-of-the-screen 6/16
12/12/2018 android - How to align views at the bottom of the screen? - Stack Overflow
'15 at 1:21

The answer above (by Janusz) is


quite correct, but I personnally don't
feel 100% confortable with
RelativeLayouts, so I prefer to
introduce a 'filler', empty TextView,
like this:

<!-- filler -->


<TextView android:layout_height="0dip"
android:layout_width="fill_p
android:layout_weight="1" />

before the element that should be at


the bottom of the screen.

answered Apr 17 '11 at 15:39


Timores
13.2k 3 38 44

Is this gonna expand to x or y axis.


Giving 0dip for height will make the
textview without any height ? or will
expand as much as it can just like to x
axis ? – Lukap Jul 18 '11 at 11:26

This expands vertically because the


height (vertical axis) is set to 0 and the
weight to 1 (assuming that other
elements have a zero weight). –
Timores Aug 10 '11 at 17:51

On the x axis, it will be as its parent


(fill_parent) – Timores Aug 10 '11 at
17:51

Rather than a textview I would use


another linear layout. Layout view
seems to imply it can be used to fill
space? – ComeIn Feb 27 '17 at 23:20

You can do this with a LinearLayout


or a ScrollView, too. Sometimes it is
easier to implement then a
RelativeLayout. The only thing you
need to do is to add the following
view before the Views you want to
align to the bottom of the screen:

<View
android:layout_width="wrap_con
android:layout_height="0dp"
android:layout_weight="1" />

This creates an empty view, filling the


empty space and pushing the next
views to the bottom of the screen.
By using our site, you acknowledge that you have read and understand our , , and our
.
edited Mar 10 '13 at 1:09

https://stackoverflow.com/questions/2386866/how-to-align-views-at-the-bottom-of-the-screen 7/16
12/12/2018 android - How to align views at the bottom of the screen? - Stack Overflow

answered Mar 10 '13 at 0:30


keybee
1,058 16 28

This also works.

<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="wrap_content
android:layout_height="fill_parent
android:layout_below="@+id/linearL
android:layout_centerHorizontal="t
android:orientation="horizontal"
android:gravity="bottom"
android:layout_alignParentBottom="
android:layout_marginTop="20dp"
>

<Button
android:id="@+id/button1"
android:layout_width="wrap_con
android:layout_height="wrap_co
android:text="Button"

/>

<Button
android:id="@+id/button2"
android:layout_width="wrap_con
android:layout_height="wrap_co
android:text="Button"

/>

</LinearLayout>

answered Dec 28 '11 at 14:03


Michael Reed
2,272 20 15

12 android:layout_alignParentBottom="tr
ue" has no effect unless the
LinearLayout is nested inside a
RelativeLayout. – Thomas Dignan
Jan 18 '12 at 15:17

By using our site, you acknowledge that you have read and understand our , , and our
.

https://stackoverflow.com/questions/2386866/how-to-align-views-at-the-bottom-of-the-screen 8/16
12/12/2018 android - How to align views at the bottom of the screen? - Stack Overflow

Following up on Timores's elegant


solution, I have found that the
following creates a vertical fill in a
vertical LinearLayout and a horizontal
fill in a horizontal LinearLayout:

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

answered Nov 15 '15 at 21:51


stevehs17
867 1 9 16

1 @stevehs17 solution does not work in


a Fragment – sepehr Jul 7 '16 at
12:42

@sepehr There must be another


factor at play in the case you found
where my solution doesn't work. I just
tried my solution in a fragment layout,
and it works there as well. –
stevehs17 Jul 11 '16 at 18:39

weights are supported in linear layouts


and @sepehr they will work in
fragments, activities, notifications,
dialogs as well ;) – Jan Rabe Jan 30
'17 at 10:10

You don't even need to nest the


second relative layout inside the
first one. Simply use the
android:layout_alignParentBottom="tr
ue" in the Button and EditText.

edited Jun 29 '11 at 11:14


Android
5,514 7 55 97

answered Mar 5 '10 at 15:05


Steve Haley
46.5k 15 67 82

Just a note that although this works


with the Button and EditText, if you
tried to align a TableLayout this way it
would not work. You would have to
nest it inside of another
RelativeLayout. – user4903 Apr 11 '11
at 20:34

1.Use ConstraintLayout in your


root Layout
By using our site, you acknowledge that you have read and understand our , , and our
And. set
app:layout_constraintBottom_toBottomO

https://stackoverflow.com/questions/2386866/how-to-align-views-at-the-bottom-of-the-screen 9/16
12/12/2018 android - How to align views at the bottom of the screen? - Stack Overflow

f="parent" to let the Layout on the


bottom of the screen

<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent
android:layout_height="wrap_conten
android:orientation="horizontal"
app:layout_constraintBottom_toBott
</LinearLayout>

2.Use FrameLayout in your root


Layout

Just set
android:layout_gravity="bottom" in
your layout

<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent
android:layout_height="wrap_conten
android:layout_gravity="bottom"
android:orientation="horizontal">
</LinearLayout>

3.Use LinearLayout in your


root Layout
( android:orientation="vertical" )

(1)Set a layout
android:layout_weight="1" on the top
of the your Layout

<TextView
android:id="@+id/TextView"
android:layout_width="match_parent
android:layout_height="0dp"
android:layout_weight="1"
android:text="welcome" />

(2)Set the child LinearLayout for


android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"

The main attribute is


ndroid:gravity="bottom" ,let the child
View on the bottom of Layout .

<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent
android:layout_height="match_paren
android:gravity="bottom"
android:orientation="horizontal">
</LinearLayout>

4.Use RelativeLayout in the


root Layout

And set
android:layout_alignParentBottom="tr
site, to
By using our ue" youlet the Layout that
acknowledge on the
you bottom of and understand our
have read , , and our
the .screen

https://stackoverflow.com/questions/2386866/how-to-align-views-at-the-bottom-of-the-screen 10/16
12/12/2018 android - How to align views at the bottom of the screen? - Stack Overflow

<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent
android:layout_height="wrap_conten
android:layout_alignParentBottom="
android:orientation="horizontal">
</LinearLayout>

OUTPUT

edited Oct 18 '17 at 11:43

answered Oct 16 '17 at 15:17


KeLiuyue
5,941 4 12 28

If you don't wish to make many


changes, then you could just put:

android:layout_weight="1"

for the TextView having ID as


@+id/TextView i.e

<TextView android:text="@string/welcom
android:id="@+id/TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_conten
android:layout_weight="1">
</TextView>

edited Oct 9 '14 at 9:58


chiwangc
3,030 11 20 30
By using our site, you acknowledge that you have read and understand our , , and our
. answered Sep 23 '11 at 12:16

https://stackoverflow.com/questions/2386866/how-to-align-views-at-the-bottom-of-the-screen 11/16
12/12/2018 android - How to align views at the bottom of the screen? - Stack Overflow
Kiran Parmar
659 7 24

Or add a surrounding LinearLayout


with android:layout_weight="1" - this
also works well inside a ScrollView! –
bk138 Nov 11 '13 at 18:49

Creating both HEADER and


FOOTER, here is an example

[ Layout XML ]

<RelativeLayout
xmlns:android="http://schemas.andr
xmlns:tools="http://schemas.androi
android:layout_width="fill_parent"
android:layout_height="fill_parent
android:background="@color/backgro
tools:context=".MainActivity">

<RelativeLayout
android:layout_width="fill_par
android:layout_height="40dp"
android:background="#FF0000">
</RelativeLayout>

<RelativeLayout
android:layout_width="fill_par
android:layout_height="40dp"
android:layout_alignParentBott
android:background="#FFFF00">
</RelativeLayout>

</RelativeLayout>

[ Screenshot ]

By using our site, you acknowledge that you have read and understand our , , and our
. this is helpful. Thanks!!
Hope

https://stackoverflow.com/questions/2386866/how-to-align-views-at-the-bottom-of-the-screen 12/16
12/12/2018 android - How to align views at the bottom of the screen? - Stack Overflow

answered May 10 '15 at 2:25


Madan Sapkota
16.2k 6 83 98

use below code align button to


bottem its working

<?xml version="1.0" encoding="utf-


<LinearLayout xmlns:android="http://sc
android:layout_width="match_parent
android:layout_height="match_paren
android:orientation="vertical" >

<Button
android:id="@+id/btn_back"
android:layout_width="100dp"
android:layout_height="80dp"
android:text="Back" />

<TextView
android:layout_width="match_pa
android:layout_height="0dp"
android:layout_weight="0.97"
android:gravity="center"
android:text="Payment Page" />

<LinearLayout
android:layout_width="match_pa
android:layout_height="wrap_co

<EditText
android:layout_width="wrap
android:layout_height="wra
android:layout_weight="1"/

<Button
android:layout_width="wrap
android:layout_height="wra
android:text="Submit"/>
</LinearLayout>

</LinearLayout>

edited Mar 23 '14 at 6:30

answered Dec 17 '13 at 7:33


sharma_kunal
1,614 21 25

For a case like this always use


RelativeLayouts. A LinearLayout is
not intended for such a usage.

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
xmlns:android="http://schemas.android.
android:id="@+id/db1_root"
android:layout_width="match_
android:layout_height="match
android:orientation="vertica
By using our site, you acknowledge that you have read and understand our , , and our
.
<LinearLayout

https://stackoverflow.com/questions/2386866/how-to-align-views-at-the-bottom-of-the-screen 13/16
12/12/2018 android - How to align views at the bottom of the screen? - Stack Overflow
android:layout_width="match_parent
android:layout_height="match_paren
android:orientation="vertical">

<!-- Place your layout here -->

</LinearLayout>

<LinearLayout
android:layout_width="match_parent
android:layout_height="wrap_conten
android:layout_alignParentBottom="
android:layout_gravity="bottom"
android:orientation="horizontal"
android:paddingLeft="20dp"
android:paddingRight="20dp" >

<Button
android:id="@+id/setup_macroSa
android:layout_width="0dp"
android:layout_height="wrap_co
android:layout_weight="1"
android:text="Save" />

<Button
android:id="@+id/setup_macroCa
android:layout_width="0dp"
android:layout_height="wrap_co
android:layout_weight="1"
android:text="Cancel" />

</LinearLayout>

</RelativeLayout>

answered May 20 '15 at 4:28


Shubham A.
1,415 17 40

Use
android:layout_alignParentBottom="tr
ue" in your <RelativeLayout> .

This will definitely help.

edited Jul 29 '14 at 20:46


Jorgesys
92k 16 236 207

answered Jul 4 '13 at 12:50


jigar
843 2 16 42

That presumes the user wants to use


a RelativeLayout. – Igor Ganapolsky
Mar 22 '16 at 12:45

In case you have a hierarchy like this:

<ScrollView>
|-- <RelativeLayout>
|-- acknowledge
By using our site, you <LinearLayout>
that you have read and understand our , , and our
.

https://stackoverflow.com/questions/2386866/how-to-align-views-at-the-bottom-of-the-screen 14/16
12/12/2018 android - How to align views at the bottom of the screen? - Stack Overflow

First, apply
android:fillViewport="true" to the
ScrollView and then apply
android:layout_alignParentBottom="tr
ue" to the LinearLayout .

This worked for me perfectly.

<ScrollView
android:layout_height="match_paren
android:layout_width="match_parent
android:scrollbars="none"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_pa
android:layout_height="wrap_co
<LinearLayout
android:orientation="horiz
android:layout_width="matc
android:layout_height="wra
android:gravity="center"
android:id="@+id/linearLay
android:layout_alignParent
</LinearLayout>
</RelativeLayout>
</ScrollView>

answered Apr 15 '15 at 8:32


Prince
15.6k 5 28 51

You can just give your top child view


(the TextView @+id/TextView) an
attribute: android:layout_weight="1" .

This will force all other elements


below it to the bottom.

answered Feb 17 '16 at 22:19


Igor Ganapolsky
14.5k 13 86 122

I used the solution Janusz posted, but


added padding to the last View since
the top part of my layout was a
ScrollView. The ScrollView will be
partly hidden as it grows with content.
Using android:paddingBottom on the
last View helps show all the content
in the ScrollView.

answered Mar 22 '12 at 7:45


jeremyvillalobos
1,392 1 15 28

This can be done with linear layout


By using our site, you acknowledge that you have read and understand our , , and our
too Just provide Height = 0dp and
.
weight = 1 to the layout above and

https://stackoverflow.com/questions/2386866/how-to-align-views-at-the-bottom-of-the-screen 15/16
12/12/2018 android - How to align views at the bottom of the screen? - Stack Overflow

the one you want in the bottom just


write height = wrap content and no
weight. What it does is it provides
wrap content for the layout (the one
that contains your edit text and
button) and then the one that has
weight occupies the rest of the layout.
I discovered this by accident.

answered Sep 5 '13 at 8:22


raihan ahmed
381 3 3

protected by Jorgesys Dec


31 '13 at 17:20
Thank you for your interest in this
question. Because it has attracted
low-quality or spam answers that
had to be removed, posting an
answer now requires 10
reputation on this site (the
association bonus does not
count).

Would you like to answer one of


these unanswered questions
instead?

By using our site, you acknowledge that you have read and understand our , , and our
.

https://stackoverflow.com/questions/2386866/how-to-align-views-at-the-bottom-of-the-screen 16/16

You might also like