jlively
jlively

Reputation: 743

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

Upvotes: 2

Views: 12709

Answers (3)

Rakesh Jha
Rakesh Jha

Reputation: 379

Use below line in scrollview and height should be match_parent.

 android:fillViewport="true"

Upvotes: 0

Siddhesh Dighe
Siddhesh Dighe

Reputation: 2954

try 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="match_parent"
    android:fitsSystemWindows="true"
    android:fillViewport="true" 
    android:orientation="vertical">

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

<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"
    />

</RelativeLayout>
</ScrollView>

Upvotes: 1

Philippe Banwarth
Philippe Banwarth

Reputation: 17735

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>

Upvotes: 27

Related Questions