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!
This question is related to
android
android-constraintlayout
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"/>
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"
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
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.
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"/>
Apparently match_parent
is :
ConstraintLayout
ConstraintLayout
So if you need your views to function as match_parent
, then:
ConstraintLayout
should use 0dp
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>
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>
match_parent
is not supported by ConstraintLayout
. Set width to 0dp
to let it match constraints.
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" />
I found one more answer when there is a constraint layout inside the scroll view then we need to put
android:fillViewport="true"
to the scroll view
and
android:layout_height="0dp"
in the constraint layout
Example:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp">
// Rest of the views
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Source: Stackoverflow.com