androidandroid-layoutlayoutandroid-studioandroid-scrollview

Impossible to add a footer to a ScrollView - Android


I'm currently working on a mobile app and I have a problem when I try to put a footer under a ScrollView.

Here is my code:

<RelativeLayout
    android:id="@+id/RelativeLayout01"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bottomcontent"
        android:orientation="vertical"
        android:background="@drawable/border">
            //the footer is added dynamically
    </RelativeLayout>

<ScrollView
    android:layout_weight="1"
    android:id="@+id/scroller"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/contentcontainer">
        <LinearLayout
            android:id="@+id/scrollcontentcontainer"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            //the content is added dynamically from a layout template
        </LinearLayout>
    </LinearLayout>
</ScrollView>
</RelativeLayout>

The content of the ScrollView is a set of relative layout with some buttons and textviews inside. It's based on a layout I'm inflating several times.

The footer is just a LinearLayout with some buttons in it as well.

The thing is I tried all the different solutions I found on Internet and none of them were working. In my case the footer is stuck under the content of the ScrollView, not under the ScrollView itsel so I have to scroll down until the content is over to reach my footer. But the footer is supposed to remain on the bottom of the screen...

I tried those solutions as well, nothing was working:

I also tried all the possible things like using gravity, weight, fillViewPort, align to the bottom... But impossible to have the expected result. The minimum API is set to 14, I use Android Studio.

edit1:

border drawable:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:bottom="-2dp"
        android:left="-2dp"
        android:right="-2dp"
        android:top="2dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="1dp"
                android:color="#000000" />

            <solid android:color="#3b010101" />

            <padding android:left="10dp"
                android:right="10dp"
                android:top="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

</layer-list>

Solution

  • You can try the following, I also had troubles with adding ScrollView inside a RelaveLayout.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <RelativeLayout
            android:id="@+id/bottomcontent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@drawable/border">
            <!---add something there eg:-->
    
          <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="Test"/>
    
         </RelativeLayout>
    
    
        <ScrollView
            android:id="@+id/scroller"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@id/bottomcontent"
            android:layout_alignParentTop="true">
    
            <LinearLayout
                android:id="@+id/contentcontainer"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <LinearLayout
                    android:id="@+id/scrollcontentcontainer"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
    
                </LinearLayout>
            </LinearLayout>
        </ScrollView>
    </RelativeLayout>
    

    enter image description here.

    I found the following in this documentation. This is causing the problem for sure

    Note: In platform version 17 and lower, RelativeLayout was affected by a measurement bug that could cause child views to be measured with incorrect MeasureSpec values. (See MeasureSpec.makeMeasureSpec for more details.) This was triggered when a RelativeLayout container was placed in a scrolling container, such as a ScrollView or HorizontalScrollView. If a custom view not equipped to properly measure with the MeasureSpec mode UNSPECIFIED was placed in a RelativeLayout, this would silently work anyway as RelativeLayout would pass a very large AT_MOST MeasureSpec instead.

    This behavior has been preserved for apps that set android:targetSdkVersion="17" or older in their manifest's uses-sdk tag for compatibility. Apps targeting SDK version 18 or newer will receive the correct behavior