Set width to match constraints in ConstraintLayout

KG6ZVP picture KG6ZVP · Jun 3, 2016 · Viewed 97k times · Source

I would like to constrain a View's left and right sides to it's parent view's margins and make it fill the allotted space. However, setting width to either match_parent or wrap_content appears to produce the same result.

Is there something equivalent to match_constraints (as opposed to match_parent and wrap_content)? Do match_parent and wrap_content affect the layout or are they ignored in the new constraint layout?

Loving this new layout system for my favorite platform!

Answer

Arieck picture Arieck · Oct 26, 2016

match_parent is not allowed. But you can actually set width and height to 0dp and set either top and bottom or left and right constraints to "parent".

So for example if you want to have the match_parent constraint on the width of the element, you can do it like this:

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"/>