fix linearlayout at the bottom as a footer in android

25,373

Solution 1

You can set the layout_height="0dp" of your header, footer and ScrollView and define a layout_weight. Just play around with the values until you find out which works best. The resulting heights of header and footer would dynamically change with the screensize.

The values 1, 5, 1 mean that the first element takes 1/7, the second 5/7 and the last 1/7 of the available height in the parent LinearLayout.

Try

<!-- HEADER -->
<TableLayout
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:layout_weight="1" >

<!-- BODY -->
<ScrollView
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:id="@+id/sw_layout"
   android:orientation="vertical"
   android:layout_weight="5">

<!-- FOOTER -->
<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:orientation="vertical"
   android:layout_weight="1" >

Solution 2

You should use RelativeLayout and in the footer layout , add the tag : android:layout_alignParentBottom="true" ; and for your ScrollView , you should put it above the footer Layout ( android:layout_above="@+id/idOfYourFooterLayout" )

Try this :

<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="wrap_content"
   android:orientation="vertical">

  <ScrollView android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:id="@+id/sw_layout"
       android:layout_above="@+id/footer"
       android:orientation="vertical">
//your UI...
  </ScrollView>

   <LinearLayout
      android:id="@+id/footer"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:orientation="vertical" >

      <Button
          android:id="@+id/btn_date"
          android:layout_height="35dp"
          android:padding="5dp"
          android:drawableLeft="@drawable/calendar"
          android:text="Tarih" />

      <Button
          android:id="@+id/btn_converter"
          android:layout_height="35dp"
          android:padding="5dp"
          android:drawableLeft="@drawable/calendar"
          android:text="Hesap" />
   </LinearLayout>
</RelativeLayout>

Solution 3

Use parent Layout as RelativeLayout and add this property android:layout_alignParentBottom="true" to the footer layout, and put the LinearLayout (footer layout ) out of scrollview. like this

<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="wrap_content"
   android:orientation="vertical">

   <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"  //// add this property
      android:orientation="vertical" >

      <Button
          android:id="@+id/btn_date"
          android:layout_height="35dp"
          android:padding="5dp"
          android:drawableLeft="@drawable/calendar"
          android:text="Tarih" />

      <Button
          android:id="@+id/btn_converter"
          android:layout_height="35dp"
          android:padding="5dp"
          android:drawableLeft="@drawable/calendar"
          android:text="Hesap" />
   </LinearLayout>
Share:
25,373
Arif YILMAZ
Author by

Arif YILMAZ

c#, mvc, web api, sql, t-sql, html5, jquery, css, angularjs

Updated on November 05, 2021

Comments

  • Arif YILMAZ
    Arif YILMAZ over 2 years

    I have done a simple layout xml file. I have been solving my issues piece by piece. I am using scrolview, linearlayout and tableview. I have a top bar and it is locked at the top. it never moves. in the middle, I have a scrollview and I need to put my last linearlayout at the bottom as a footer and I dont want it be disappear if the user scrolls down. I want it to be locked there.

    here is my code

    <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    
       <TableLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent">
    
             <TableRow
            android:background="@drawable/gradient"
            android:gravity="center">
    
                .......
    
             </TableRow>
    
       </TableLayout>
    
        <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/sw_layout"
    android:orientation="vertical">
    
          <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical" >
    
        <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:background="#EEEEEE" >
    
            <TableRow
            android:id="@+id/tableRowDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
    
                ..........
    
                </TableRow>
    
            </TableLayout>
    
            /*****************************************/
            /* I want this to be footer at the bottom*/
            /*****************************************/
            <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <Button
                android:id="@+id/btn_date"
                android:layout_height="35dp"
                android:padding="5dp"
                android:drawableLeft="@drawable/calendar"
                android:text="Tarih" />
    
                <Button
                android:id="@+id/btn_converter"
                android:layout_height="35dp"
                android:padding="5dp"
                android:drawableLeft="@drawable/calendar"
                android:text="Hesap" />
              </LinearLayout>
            /*****************************************/
            /* I want this to be footer at the bottom*/
            /*****************************************/
    
           </LinearLayout>
         </ScrollView>  
     </LinearLayout>
    

    How can I achive to have footer locked at the bottom?