raddevus
raddevus

Reputation: 9077

Why doesn't the the TextInputLayout follow the width of parent ConstraintLayout?

You can see here that I have a CardView which is the parent of a ConstraintLayout.

componentTree view

Additionally, the CardView layout_width is set to match_parent. And the ConstraintLayout is set to match_parent. That all works because the CardView and ConstraintLayout are the width of the entire layout area.

The Problem You can also see that I have a TextInputLayout which is the child of the ConstraintLayout. The only setting choice I have for layout_width is match_constraint (shown in following image).

textinputlayout layout_width

When I choose match_constraint the value automatically gets set to 0dp and the widget ends up being 0dp on screen.

Is This A Bug?

Why doesn't the the TextInputLayout follow the width of the ConstraintLayout (width of entire layout) -- similar to match_parent? Can you advise the proper way to fix this? Is there a different value that the TextInputLayout's layout_width should be set to?

Upvotes: 1

Views: 566

Answers (2)

Pawel Laskowski
Pawel Laskowski

Reputation: 6346

You should not use match_parent for Views which are children of ConstraintLayout as stated in the documentation:

Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout. Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".

What you're missing in your case is the end constraint for your TextInputLayout. To use 0dp (match_constraint) you need to have both constraints specified for that dimension:

<android.support.design.widget.TextInputLayout
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:id="@+id/description_layout"
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintStart_toStartOf="parent"
     app:layout_constraintEnd_toEndOf="parent" />

Upvotes: 2

raddevus
raddevus

Reputation: 9077

If I look at the layout's XML i see the following for the TextInputLayout

<android.support.design.widget.TextInputLayout
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:id="@+id/description_layout"
    app:layout_constraintTop_toTopOf="parent" 
app:layout_constraintStart_toStartOf="parent">

If I change layout_width like the following (by manually typing the value in):

android:layout_width="match_parent"

Then the layout looks like I expect. This seems to fix the problem. Is it correct?

enter image description here

Upvotes: 1

Related Questions