androidxmlandroid-layoutandroid-scrollview

How to put button on the bottom of Scrollview


I've tried searching all over, but I can't seem to figure this one out. Basically, I have a layout with Scrollview and elements inside it. I want to put a button inside the scrollview which would be at the end/bottom and you can only see it when you scroll all the way to the bottom. I've tried doing it like this:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/topcontainer">

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/toolbar_review"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:title="Review Your Massage">

        </android.support.v7.widget.Toolbar>


        <include layout="@layout/card_review"/>

    </LinearLayout>


    <Button
        android:id="@+id/buttonReview"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:background="@color/colorAccent"
        android:text="@string/book"
        android:layout_below="@+id/topcontainer"
        />
</RelativeLayout>
</ScrollView>

The button is indeed at the bottom but there is space beneath it. If I run the app on a phone with bigger resolution, the button will be almost in the middle of the screen. How can I make it so no matter the resolution it will always stay on bottom?

Here's a screenshot of what it looks like at the moment:enter image description here


Solution

  • With android:layout_height="match_parent" and android:fillViewport="true" the ScrollView uses all the available space, even if the children are smaller.

    The Space has a android:layout_weight="1" to receive the extra space, so the button is always at the bottom.

    An explanation by Romain Guy with more details.

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:fillViewport="true"
                android:orientation="vertical">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/topcontainer">
    
                <android.support.v7.widget.Toolbar
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/toolbar_review"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/colorPrimary"
                    android:minHeight="?attr/actionBarSize"
                    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                    app:title="Review Your Massage">
    
                </android.support.v7.widget.Toolbar>
    
                <include layout="@layout/card_review"/>
    
                <android.support.v4.widget.Space
                    android:layout_width="match_parent"
                    android:layout_weight="1"
                    android:layout_height="0dp" />
    
                <Button
                    android:id="@+id/buttonReview"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:background="@color/colorAccent"
                    android:text="@string/book" />
            </LinearLayout>
    
    </ScrollView>