fix linearlayout at the bottom as a footer in android
Asked Answered
A

6

5

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?

Adaptation answered 18/6, 2013 at 9:10 Comment(5)
you can make another xml file with linearlayout and include it in your xml codeGiaour
but how can I do that?Adaptation
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"Paulin
look below i added answerGiaour
@ayilmaz : see my answer belowPaulin
C
2

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" >
Caroleecarolin answered 18/6, 2013 at 9:24 Comment(0)
P
20

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>
Paulin answered 18/6, 2013 at 9:30 Comment(2)
what if the footer should be inside the ScrollView ?Bookworm
@FranciscoCorralesMorales : Just use a RelativeLayout as a root layout inside your ScrollView. and add the android:layout_alignParentBottom="true"to the view you want it to be a footer of your UI.Paulin
C
2

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" >
Caroleecarolin answered 18/6, 2013 at 9:24 Comment(0)
P
1

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>
Photovoltaic answered 18/6, 2013 at 9:26 Comment(0)
G
0

use one xml like this- >>

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


</LinearLayout>

and include it as

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

  ...

  <!-- Overridden attributes never work. Nor do attributes like
       the red background, which is specified here. -->
  <include
      android:id="@+id/buttons_override"
      android:background="#ff0000"
      android:layout_width="fill_parent"
      layout="@layout/buttons"/>

</LinearLayout>
Giaour answered 18/6, 2013 at 9:19 Comment(0)
C
0

Use Relative layout as your parent layout then add your three child views as follows

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" >
    <View android:id="@+id/top_view"
     android:alignParentTop="true">  // Your view that as to be in Top 
    </View> 

     <ScrollView android:layout_below ="@id/top_view" > //scroll view in middle below topview
     </ScrollView>

    <View android:alignParentBotton="true">  //Your expected view in bottom
    </View>

</RelativeLayout>
Cyclades answered 18/6, 2013 at 9:51 Comment(0)
B
0

In my case, I have added these lines of code below in the Oncreate function and top of the Layout

like below

enter image description here

code is here:

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
Boutwell answered 4/11, 2021 at 10:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.