11

I want a button to appear at fixed location all the time, in the footer of the UI ؟ ( always, if it has components above it or not )

Sujit
  • 10,137
  • 9
  • 38
  • 43
Adham
  • 61,516
  • 96
  • 221
  • 343

5 Answers5

32

Please take one Relative layout under your main layout . Set its height and width as fill parent and set its gravity as bottom and put any textview or any button you want in it.

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">

    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:gravity="bottom">

        <Button 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="Bottom Gravity" />

    </RelativeLayout>

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

        <Button 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="Without Gravity" />

    </LinearLayout>

</FrameLayout>

enter image description here

Mark
  • 6,448
  • 5
  • 24
  • 45
Chirag
  • 56,384
  • 29
  • 154
  • 197
  • 1
    Perfect Answer. Works. But should use "match_parent" instead of "fill_parent" – rjdthegreat Jan 09 '16 at 20:53
  • It works perfectly, but any clues if there is anyway to the button being not elevated by keyboard? – vinicius gati Jun 14 '18 at 11:56
  • thanks a lot. It worked for me. I had the nesting of Linearlayout. One of them I changed to RelativeLayout with the attributes and values as you specified and It worked perfectly. – Vijay Aug 17 '19 at 14:18
6

It depends on the layout you are using.

On a RelativeLayout there is

android:layout_alignParentBottom="true"

On a LinearLayout, place it at the bottom and make sure to set the elements layout_weight properly.

Also, check the property

android:layout_gravity

and notice it's different than

android:gravity
Maragues
  • 36,772
  • 14
  • 93
  • 96
  • i confirm your answer , he should use a relativeLayout to fixe the button at the bottom with `android:layout_alignParentBottom="true"`, and i want just to add that if he want to keep his button to the front , in his code he can call the method : `btn.bringToFront()` – Houcine Jun 29 '11 at 11:00
2

Set android:layout_gravity="bottom". Hope this helps.

Egor
  • 38,538
  • 10
  • 111
  • 128
1

Put

android:layout_alignParentBottom="true"

in your relative layout.

jigar
  • 1,491
  • 6
  • 23
  • 46
1

if any one have two button you can just make this it orks for me

<RelativeLayout 
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:gravity="bottom">

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

            <android.support.v7.widget.AppCompatButton
                android:id="@+id/btn_yes"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="12dp"
                android:text="Valider"/>

            <android.support.v7.widget.AppCompatButton
                android:id="@+id/btn_no"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="12dp"
                android:text="Annuler"/>
     </LinearLayout>

  </RelativeLayout>`
na3na3iss
  • 55
  • 1
  • 10