123

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
KG6ZVP
  • 3,610
  • 4
  • 26
  • 45

10 Answers10

194

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"/>
Arieck
  • 2,002
  • 2
  • 9
  • 8
  • 9
    Android team should make "match_parent" work. It would be extremely easy. Simply make match_parent provide the same functionality as if the app was constrained to start and end of parent. Would be easy to detect vertically or horizontally. – bharv14 Apr 26 '18 at 00:36
  • But doesn't 0dp result in double-measure? – Pramod Garg Jun 06 '18 at 06:08
160

match_parent is not supported, so use android:layout_width="0dp". With 0dp, you can think of your constraints as 'scalable' rather than 'filling whats left'.

Also, 0dp can be defined by a position, where match_parent relies on it's parent for it's position (x,y and width, height)

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
rsteg
  • 1,710
  • 1
  • 10
  • 6
  • 37
    why is this accepted answer?!? match_parent is not supported by constraint layout. And this answer give no way of implementing it. – Daniele Segato Sep 21 '16 at 16:57
  • 3
    match_parent is not supported. – Nicolas Roard Jan 12 '17 at 23:11
  • 12
    This answer is correct. It clearly states match_parent is not supported. Also, to perform a reasonable alternative to a 'match_parent' setting, 0dp with constraints set to parent on left and right (margin 0 or pick to suit) will give the same result. The only thing that was really left out in this answer, which is in Arieck's answer, is the need to set constraints on both sides (or top and bottom for vertical). This is how I do it and i have not had any issues. Plus it works as a weight setting when used with other components. – Eric Engel Mar 10 '17 at 18:12
  • 1
    If someone comes here seeking the answer for it not working. It seems that it got broken in beta. It seems to work in 1.0.2. Lesson learned - freeze the libraries, don't use opportunistic updates. – inteist Mar 09 '18 at 14:47
  • 1
    @Tequilaman comment can be the answer. He has rightly pointed out the way to implement it. Create a constrain on either sides with 0dp margin, it does the trick. – Utkarsh Mankad Nov 15 '18 at 06:10
32

Apparently match_parent is :

  • NOT OK for views directly under ConstraintLayout
  • OK for views nested inside of views that are directly under ConstraintLayout

So if you need your views to function as match_parent, then:

  1. Direct children of ConstraintLayout should use 0dp
  2. Nested elements (eg, grandchild to ConstraintLayout) can use match_parent

Example:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="16dp">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/phoneNumberInputLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/phoneNumber"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </android.support.design.widget.TextInputLayout>
Gene Bo
  • 11,284
  • 8
  • 90
  • 137
19

match_parent is not supported by ConstraintLayout. Set width to 0dp to let it match constraints.

Marcin Koziński
  • 10,835
  • 3
  • 47
  • 61
18

From the official doc:

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".

So if you want achieve MATCH_PARENT effect, you can do this:

<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="TextView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
xymelon
  • 315
  • 2
  • 7
  • 1
    Note that the doc as of 2021 is even stronger. It's no longer worded as a "recommendation": https://developer.android.com/training/constraint-layout Note: You cannot use match_parent for any view in a ConstraintLayout. Instead use "match constraints" (0dp). – Carmen Oct 11 '21 at 09:35
8

You can check your Adapter.

 1 - MyLayoutBinding binding = MyLayoutBinding.inflate(layoutInflater);
 2 - MyLayoutBinding binding = MyLayoutBinding.inflate(layoutInflater, viewGroup, false);

I had a same problem like you when I used 1. You can try 2.

MashukKhan
  • 1,946
  • 1
  • 27
  • 46
Kim Jeong woo
  • 81
  • 1
  • 4
3

For making your view as match_parent is not possible directly, but we can do it in a little different way, but don't forget to use Left and Right attribute with Start and End, coz if you use RTL support, it will be needed.

    <Button
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
3

in the office doc: https://developer.android.com/reference/android/support/constraint/ConstraintLayout

When a dimension is set to MATCH_CONSTRAINT, the default behavior is to have the resulting size take all the available space.

Using 0dp, which is the equivalent of "MATCH_CONSTRAINT"

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"

2

set width or height(what ever u need to match parent ) to 0dp and set margins of left , right, top, bottom to act as match parent

santhosh
  • 31
  • 7
-1

If you want TextView in the center of parent..
Your main layout is Constraint Layout

<androidx.appcompat.widget.AppCompatTextView
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:text="@string/logout"
     app:layout_constraintLeft_toLeftOf="parent"
     app:layout_constraintRight_toRightOf="parent"
     android:gravity="center">
</androidx.appcompat.widget.AppCompatTextView>
Taazar
  • 1,545
  • 18
  • 27