1

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

jlively
  • 655
  • 1
  • 9
  • 26
  • remove android:layout_alignParentBottom="true" – Pavya Jun 16 '16 at 13:57
  • set Height of ScrollView = "match_parent" & not "wrap_content" same for RelativeLayout – Luca Nicoletti Jun 16 '16 at 13:57
  • and add Button below . Remove Relative layout – Pavya Jun 16 '16 at 13:58
  • 1. Removed android:layout_alignParentBottom="true" and added the button below . Also removed the Relative Layout. I still get the same result. 2. I set Height of ScrollView = "match_parent" and I do the same for Relative Layout. I still get the same result. – jlively Jun 16 '16 at 14:08

2 Answers2

17

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>
bwt
  • 13,696
  • 34
  • 53
1

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>
Siddhesh Dighe
  • 2,504
  • 2
  • 11
  • 15
  • If I use this on lets say Nexus 6P, it works, the button is on bottom. But, if I use it on Nexus 4, the content is not scrollable and the button is always shown. I need it to be shown only when you scroll down to it. – jlively Jun 16 '16 at 15:42
  • You can try, hiding the button and making it visible when the scrollView reaches the bottom. heres a link which explains how to detect when scrollView hits the bottom. http://stackoverflow.com/questions/10316743/detect-end-of-scrollview – Siddhesh Dighe Jun 16 '16 at 16:16