user3777879
user3777879

Reputation: 41

Fit Scroll View to Available Space in Android

I have a problem with my ScrollView. I want to fit it inside the space available but i don´t know how.

Now i have my ScrollView and my TextView before one LinearLayout:

        <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:fillViewport="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/previousimage"
        android:layout_marginTop="@dimen/margin">


        <TextView
            android:id="@+id/question_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margintop"
            android:gravity="center_horizontal"
            android:paddingBottom="@dimen/margin_five"
            android:scrollbars="vertical"
            android:textColor="@color/white"
            android:textSize="@dimen/common_textsize" />

    </ScrollView>
    <LinearLayout
        android:id="@+id/bottom_options"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/scrollView"
        android:layout_marginTop="@dimen/margin_small"
        android:gravity="center|bottom"
        android:orientation="vertical">

The problem is that if i have so much text inside my TextView the LinearLayout go down and it hides.I want to scroll the text inside. Without push down the LinearLayout.

I want to take this space for ScrollView automatically to adapt to all screen size.

See the image Layaout

Thank you!

Upvotes: 1

Views: 2719

Answers (2)

i_A_mok
i_A_mok

Reputation: 2904

Define LinearLayout before your ScrollView and set ScrollView to above LinearLayout, match_parent. Like this:

<LinearLayout
    android:id="@+id/bottom_options"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginTop="@dimen/margin_small"
    android:gravity="center|bottom"
    android:orientation="vertical">
    ...
</LinearLayout>

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:fillViewport="true"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/previousimage"
    android:layout_above="@+id/bottom_options"
    android:layout_marginTop="@dimen/margin">

    <TextView
        android:id="@+id/question_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margintop"
        android:gravity="center_horizontal"
        android:paddingBottom="@dimen/margin_five"
        android:scrollbars="vertical"
        android:textColor="@color/white"
        android:textSize="@dimen/common_textsize" />

</ScrollView>

Upvotes: 4

Vadim Caen
Vadim Caen

Reputation: 1636

The ScrollView should always match_parent (or at least be smaller than the available space) otherwise it just take the same space as its children and so from its perspective, no children is out, so no need to scroll.

Upvotes: 0

Related Questions