Thursday, 10 October 2019

How can I use ConstraintLayout to have the first View shrink to fit remaining space?

I have the following layout. Notice Z is positioned below Y, but constrained to the bottom. There is a nice gap gap between Y and Z, afforded by the excess vertical space. This is my desired and actual behavior when there is excess vertical space.

enter image description here

However, I run out of excess vertical space when the keyboard is shown.

Desired Behavior (no excess vertical space) When I run out of vertical space, I would like the following to occur: X (ScrollView), shrinks to fill the remaining space, allowing Y and Z to be displayed at full size.

enter image description here

Actual Behavior (no excess vertical space) Y shrinks instead.

enter image description here

My source is below. How can I modify it to get my desired behavior in both scenarios (excess vertical space and no excess vertical space)?

<androidx.constraintlayout.widget.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">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#fbe9e7"
            android:gravity="center"
            android:text="X"
            android:textSize="96sp">

        </TextView>
    </ScrollView>

    <TextView
        android:id="@+id/text_Y"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f3e5f5"
        android:gravity="center"
        android:text="Y"
        android:textSize="96sp"
        app:layout_constraintTop_toBottomOf="@+id/scrollView" />

    <TextView
        android:id="@+id/text_Z"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#e1f5fe"
        android:gravity="center"
        android:text="Z"
        android:textSize="96sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text_Y"
        app:layout_constraintVertical_bias="1" />

</androidx.constraintlayout.widget.ConstraintLayout>

The issue largely stems from the X scrollview needing to be 0dp when vertical space is limited, but wrap_content when there is excess vertical space



from How can I use ConstraintLayout to have the first View shrink to fit remaining space?

No comments:

Post a Comment