Felipe Castilhos
Felipe Castilhos

Reputation: 826

Right way to have a scrollable bottom view

What is the right way to have a scrollable bottom view?

<ScrollView 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"
android:fillViewport="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/wrnTextP1"
        android:text="Ok , batatas  "/>

    <android.support.v4.widget.Space
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="1" />

    <Button
        style="@style/FullWidthButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Chega por hoje!" />
</LinearLayout>

at the preview its looke fine, but when I run it on the emulator the view(in this case: the button) its not at bottom at all.

enter image description here

Upvotes: 0

Views: 91

Answers (1)

Ben P.
Ben P.

Reputation: 54204

I think you are misunderstanding how ScrollView is meant to be used. The LinearLayout child of your ScrollView has a match_parent height... if your ScrollView is the same height as the screen, and the LinearLayout is the same height as the ScrollView, then there's nothing to scroll (all the content is the size of the screen).

The direct child of a ScrollView should either have a fixed height (like 2000dp) or use wrap_content.

If you use a fixed height, then the rest of your code "works"; you can have a Space element that uses layout_weight to push the last view down to the bottom of your LinearLayout. However, if you're using wrap_content, this is impossible (since there's no "extra" space for the layout_weight to consume).

Instead, you could consider putting the "bottom" view outside of the ScrollView. Something like this:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        ...

    </ScrollView>

    <Button
        style="@style/FullWidthButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Chega por hoje!" />

</LinearLayout>

Upvotes: 1

Related Questions