2

How to display widget button at bottom of screen.

I want to display Send button at the bottom of the screen.

Attaching the screenshot :

enter image description here

The following the is the layout xml :

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

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:hint="@string/to"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:hint="@string/subject"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:hint="@string/message"/>

    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:text="@string/send"/>

</LinearLayout>

Check the button (Send) above i want to display this button at bottom of the screen. what should i do. I have used android:layout_gravity=0.0 for the button widget, but not works.

vermaraj
  • 634
  • 3
  • 10
  • 34
  • 4
    You should use a `RelativeLayout` instead of the `LinearLayout` you are using now, and there you can set `android:alignParentBottom="true"`. – pshegger Aug 06 '14 at 11:46
  • Cant we do that in Linear Layout. – vermaraj Aug 06 '14 at 11:47
  • @vermaraj No..you need to use RelativeLayout for this. – kalyan pvs Aug 06 '14 at 11:48
  • 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) – avalancha Aug 06 '14 at 11:53
  • 1
    Dude this is literelly the **first** thing you find in **any** tutorial about Android, do the minimal research before posting! – avalancha Aug 06 '14 at 11:54
  • @avalancha I did this but i want to do this with Linear Layout. Because there may be various changes in the layout xml. this is just a sample. That's why i tried to do it in Linear layout. – vermaraj Aug 06 '14 at 11:55
  • @vermaraj if you know we can do this in linear layout then i am curious to know how – jaimin Aug 06 '14 at 11:56
  • 2
    Well that's not really an excuse now is it? [Link](http://stackoverflow.com/questions/14779688/put-buttons-at-bottom-of-screen-with-linearlayout), [Link](http://stackoverflow.com/questions/6575409/linearlayout-layout-gravity-bottom-not-working-on-horizontal-linearlayout)... Can all be found on the **1st page** [here](https://www.google.com/?gws_rd=ssl#q=linearlayout+align+bottom) – avalancha Aug 06 '14 at 12:00
  • @vermaraj,is my solution not solve your problem ? – Haresh Chhelana Aug 06 '14 at 12:13

3 Answers3

3

Try this way,hope this will help you to solve your problem.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical" >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/to"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/subject"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/message"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="bottom|right">
        <Button
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="@string/send"/>
    </LinearLayout>

</LinearLayout>
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
2

Use Relative Layout instead of LinearLayout easily:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/to"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:hint="to"/>

    <EditText
        android:id="@+id/sub"
        android:layout_below="@id/to"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:hint="subject"/>

    <EditText
        android:id="@+id/msg"
        android:layout_below="@id/sub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:hint="message"/>

    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="send"/>

</RelativeLayout>

In LinearLayout, you won't be able to do this. Hope it helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • 2
    Of course you're [able to](http://stackoverflow.com/questions/6575409/linearlayout-layout-gravity-bottom-not-working-on-horizontal-linearlayout), if you're answering a duplicate question please do the reasearch – avalancha Aug 06 '14 at 12:06
1

You need to use Relative Layout For you Layout file

and Change

<Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:text="@string/send"/>

Via

<Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="@string/send"/>
Krupa Patel
  • 3,309
  • 3
  • 23
  • 28