1

I have created layout like this one

<?xml version="1.0" encoding="utf-8"?>
<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.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/top"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/gray"
        android:gravity="center"
        android:padding="10dp"
        android:text="top"
        android:textColor="@color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/middle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/green"
        android:gravity="center"
        android:minHeight="300dp"
        android:padding="10dp"
        android:text="middle"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@id/bottom"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/top" />

    <TextView
        android:id="@+id/bottom"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/gray"
        android:gravity="center"
        android:padding="10dp"
        android:text="bottom"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

It does scroll on small screens as expected. But on larger screens it leaves space below enter image description here

How can I make layout match screen height, but still be scrollable on small screens?

Tried to set 'middle' height to 0dp, but this did not help.

Adding android:fillViewport="true" to ScrollView and set middle's height to 0dp solves the problem with space, but brings problem with scrolling - on small screens middle is shrinking, instead of scrolling.

enter image description here

Alexey
  • 2,980
  • 4
  • 30
  • 53

2 Answers2

4

Set the height of the middle TextView to 0dp (to match constraints) to fill the available space and add android:fillViewport="true" attribute to your ScrollView to stretch its content to fill the viewport.

<?xml version="1.0" encoding="utf-8"?>
<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:fillViewport="true">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/top"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/gray"
            android:gravity="center"
            android:padding="10dp"
            android:text="top"
            android:textColor="@color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/middle"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/green"
            android:gravity="center"
            android:padding="10dp"
            android:text="middle"
            android:textColor="@color/white"
            app:layout_constraintHeight_min="300dp"
            app:layout_constraintBottom_toTopOf="@id/bottom"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/top" />

        <TextView
            android:id="@+id/bottom"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/gray"
            android:gravity="center"
            android:padding="10dp"
            android:text="bottom"
            android:textColor="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

    </android.support.constraint.ConstraintLayout>
</ScrollView>
Pawel Laskowski
  • 6,078
  • 1
  • 21
  • 35
0

The solution is to use app:layout_constraintHeight_min instead of android:minHeight

So this layout is correct

<?xml version="1.0" encoding="utf-8"?>
<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:fillViewport="true">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/top"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/gray"
        android:gravity="center"
        android:padding="10dp"
        android:text="top"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@id/middle"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="spread_inside" />

    <TextView
        android:id="@+id/middle"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/green"
        android:gravity="center"
        android:padding="10dp"
        android:text="middle"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@id/bottom"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_min="300dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/top" />

    <TextView
        android:id="@+id/bottom"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/gray"
        android:gravity="center"
        android:padding="10dp"
        android:text="bottom"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/middle" />

</android.support.constraint.ConstraintLayout>

Alexey
  • 2,980
  • 4
  • 30
  • 53