androidandroid-constraintlayoutconstraint-layout-chains

Chained view displaced in ConstraintLayout


I've encountered a problem with ConstraintLayout while simply placing 2 views in the center of screen. Like Title and Content bellow. Both should be centered vertically. So my layout looks like:

<?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”>

   <TextView
       android:id=“@+id/title”
       android:layout_width=“0dp”
       android:layout_height=“wrap_content”
       android:text=“Title”
       android:textAppearance=“@style/Title1"
       app:layout_constraintBottom_toTopOf=“@id/content”
       app:layout_constraintEnd_toEndOf=“parent”
       app:layout_constraintStart_toStartOf=“parent”
       app:layout_constraintTop_toTopOf=“parent”
       app:layout_constraintVertical_chainStyle=“packed” />

   <EditText
       android:id=“@+id/content”
       android:layout_width=“match_parent”
       android:layout_height=“wrap_content”
       android:inputType=“text|textMultiLine|textNoSuggestions”
       android:textAppearance=“@style/Title4"
       app:layout_constraintBottom_toBottomOf=“parent”
       app:layout_constraintEnd_toEndOf=“parent”
       app:layout_constraintStart_toStartOf=“parent”
       app:layout_constraintTop_toBottomOf=“@id/title”
       tools:text=“body\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\nbody\n” />

</androidx.constraintlayout.widget.ConstraintLayout>

But when Content growth Title is moved out of screen bounds. It looks like a bug. How can I avoid this behavior and make views to be inside screen bounds? Title view displaced


Solution

  • By default, Views that have wrap_content set for a dimension do not have their constraints enforced when the content gets too big to fit them. To change this behavior and limit the dimension you need to set app:layout_constrainedHeight="true" for your EditText.

    As a side note, you should also avoid using match_parent for Views contained in ConstraintLayout as stated in the documentation.