asdjfiasd
asdjfiasd

Reputation: 1730

How to make TextView fill space vertically in ScrollView?

I have following layout. I have FrameLayout (fuchsia) filling entire screen and on the left I have ScrollView (silver). In ScrollView is LinearLayout (lime) with buttons. Above bottom button is TextView spacer. I want it to fill vertical space so that bottom button is at the bottom of screen. With scroll view scrolling works but spacer is not stretched:

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff00ff">

    <ScrollView
        android:layout_width="134dp"
        android:layout_height="match_parent"
        android:background="#aaaaaa">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00ff00"
            android:orientation="vertical">

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Top" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="1" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="2" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="3" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="4" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="5" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="6" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="7" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="Spacer" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Bottom" />
        </LinearLayout>
    </ScrollView>
</FrameLayout>

If I remove ScollView then spacer is stretched but scrolling doesn't work in landscape mode:

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff00ff">

<LinearLayout
    android:layout_width="134dp"
    android:layout_height="match_parent"
    android:background="#00ff00"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Top" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="1" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="2" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="3" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="4" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="5" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="6" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="7" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="Spacer" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Bottom" />
    </LinearLayout>
</FrameLayout>

Upvotes: 0

Views: 708

Answers (2)

Prajwal Waingankar
Prajwal Waingankar

Reputation: 2710

Solution:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff00ff">

    <ScrollView
        android:layout_width="134dp"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:background="#aaaaaa">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00ff00"
            android:orientation="vertical">

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Top" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="1" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="2" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="3" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="4" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="5" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="6" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="7" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="Spacer" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Bottom" />
        </LinearLayout>
    </ScrollView>
</FrameLayout>

Setting android:fillViewport="true" on the ScrollView fixed your issue and setting the height of ScrollView to match_parent.

Upvotes: 2

Rahul
Rahul

Reputation: 631

I think constraint layout will help you.

<ConstraintLayout 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:orientation="vertical">

    <ScrollView
        android:layout_width="134dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/btnBottom"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="134dp"
            android:layout_height="wrap_content"
            android:background="#00ff00"
            android:orientation="vertical">

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Top" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="1" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="2" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="3" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="4" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="5" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="6" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="7" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="Spacer" />
        </LinearLayout>

    </ScrollView>

    <Button
        android:id="@+id/btnBottom"
        android:layout_width="134dp"
        android:layout_height="wrap_content"
        android:text="Bottom"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</ConstraintLayout>

Upvotes: 0

Related Questions