ConstraintLayout: layout_constrainedHeight="true" makes my View too small to wrap content

Viewed 5440

I am trying to make a layout where the user reads some text, and then types in an EditText. The text should fill the available space as much as possible, while always showing the EditText and Button.

When the user clicks the EditText, and the keyboard opens, the TextView should resize and allow the user to scroll the text.

I have almost achieved this using Constraint Layout chains, as shown below:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<include
    android:id="@+id/include"
    layout="@layout/somelayout"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/explanationText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginEnd="24dp"
    android:layout_marginLeft="24dp"
    android:layout_marginRight="24dp"
    android:layout_marginStart="24dp"
    android:layout_marginTop="23dp"
    android:scrollbars="vertical"
    android:text="@string/sometext"
    app:layout_constrainedHeight="true"
    app:layout_constraintBottom_toTopOf="@+id/emailInput"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_max="0dp"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/include"
    app:layout_constraintVertical_bias="0.0"
    app:layout_constraintVertical_chainStyle="packed" />

<EditText
    android:id="@+id/emailInput"
    style="@style/email_edit_text"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:layout_marginEnd="24dp"
    android:layout_marginLeft="24dp"
    android:layout_marginRight="24dp"
    android:layout_marginStart="24dp"
    android:layout_marginTop="25dp"
    android:hint="@string/onboarding_user_email_hint"
    app:layout_constraintBottom_toTopOf="@+id/saveButton"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/explanationText"
    tools:ignore="Autofill" />

<Button
    android:id="@+id/saveButton"
    style="@style/raised_button_grey"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="40dp"
    android:width="127dp"
    android:text="@string/save"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

When the user opens the keyboard, the TextView indeed shrinks to fill the available space, and the text becomes scrollable. Great!

However, there's one small bug. Even when the TextView has plenty of space to fill, it doesn't wrap content - the height of the TextView is always 1 or 2 lines too small, forcing the text to scroll.

It seems like the ConstraintLayout isn't measuring the height of the content of the TextView exactly right, so the TextView is just too short to properly wrap_content. Does anyone know a fix?

0 Answers
Related