Set width to match constraints in ConstraintLayout
AndroidAndroid ConstraintlayoutAndroid Problem Overview
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?
Android Solutions
Solution 1 - Android
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"/>
Solution 2 - Android
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)
Solution 3 - Android
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:
- Direct children of
ConstraintLayout
should use0dp
- 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>
Solution 4 - Android
match_parent
is not supported by ConstraintLayout
. Set width to 0dp
to let it match constraints.
Solution 5 - Android
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" />
Solution 6 - Android
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.
Solution 7 - Android
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"/>
Solution 8 - Android
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
Solution 9 - Android
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"
Solution 10 - Android
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>