Home » Android » How to create fixed footer in android layout?

How to create fixed footer in android layout?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am using following code to display button at the bottom of activity.

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

         android:text="Get more"
       />
       </RelativeLayout> 

and listview above it. when i display more data in listview this button pannel is moved down.can any one guide me how can i fix it at the bottom of activity?

any help would be appreciated.

How to&Answers:

The android:layout_alignParentBottom attribute has to be declared in an element of the RelativeLayout not in the RelativeLayout himself (unless there is another RelativeLayout as a parent).

You should do something like this, with the ListView inside the RelativeLayout also :

<RelativeLayout 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"         
 android:layout_centerHorizontal="true">
  <ListView ...>
  <Button android:id="@+id/btnGetMoreResults"
   android:layout_height="wrap_content" 
   android:layout_width="wrap_content"     
   android:text="Get more"
   android:layout_alignParentBottom="true" />
</RelativeLayout> 

Answer:

The answer selected as correct is faulty, the button will hide the lower part of the list view. The correct way is to declare the button first and position the list above the button.

<Button android:id="@+id/btnGetMoreResults"
   android:layout_height="wrap_content" 
   android:layout_width="wrap_content"     
   android:text="Get more"
   android:layout_alignParentBottom="true"/>

<ListView 
   ...
   android:layout_above="@id/btnGetMoreResults"/>

Answer:

If you had, for example, all the scrollable elements in a ScrollView, you should do like the following:

<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="match_parent"
        style="@style/rootElement"
    >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

            <!-- texts, buttons, images and anything that you want to scroll -->

        </ScrollView>

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

    </LinearLayout>

</RelativeLayout>

Note that if you want the footer to be fixed, then you shouldn’t put it in the ScrollView, where the scrollable content will be placed. Make it child of RelativeLayout and set layout_alignParentBottom to true. Maybe you’ll need to add a padding at the bottom of the ScrollView in this case (so that the last element do not get hidden by the footer).

The idea is similar for elements other than ScrollView