constraintlayout
constraintlayout copied to clipboard
layout_constraintWidth_max ignored when layout_constraintWidth_percent also specified
In constraintlayout version 1.1.3 the following layout specification used to work:
android:layout_width="0dp"
app:layout_constraintWidth_max="200dp"
app:layout_constraintWidth_percent="0.5"
It set the width to 50% but it would max out at 200dp. Specifying layout_constraintWidth_max="wrap" was also possible, it resulted in 50% of the available width as long as it did not exceed the component's natural width.
As of constraintlayout version 2.0.0 layout_constraintWidth_max seems to be completely ignored and the component's width is set to 50% unconditionally. The same is probably true for the corresponding height attributes.
I believe the previous behavior was quite useful.
Which version of 2.0 are you using? trying to replicate it it seems to work. Could you share a full layout example? Thanks
It turns out the issue is more complicated than I thought. One view alone works just fine but if you add a second view that depends on the location of the first one the layout breaks. I'm using version 2.0.4.
Here's a basic example that works as expected:
<androidx.constraintlayout.widget.ConstraintLayout
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">
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#40f02020"
android:text="Some text"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintWidth_max="wrap"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
If you add a second view that depends on the first one the layout breaks:
<androidx.constraintlayout.widget.ConstraintLayout
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">
<TextView
android:id="@+id/title"
android:background="#4020f020"
android:text="Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/text"
app:layout_constraintLeft_toLeftOf="@+id/text"
app:layout_constraintRight_toRightOf="@+id/text"/>
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#40f02020"
android:text="Some text"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintWidth_max="wrap"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Also note that the resulting layout will look different if you change the order of declaration:
<androidx.constraintlayout.widget.ConstraintLayout
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">
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#40f02020"
android:text="Some text"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintWidth_max="wrap"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<TextView
android:id="@+id/title"
android:background="#4020f020"
android:text="Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/text"
app:layout_constraintLeft_toLeftOf="@+id/text"
app:layout_constraintRight_toRightOf="@+id/text"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Both layouts worked as expected in version 1.1.3.
Ah great -- thanks for the update, I can reproduce. Let me look into it.
Fixed in tip of tree
When will this fix be released? I have tested with 2.1.0-beta02 and it still does not work.
Sample:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:background="@color/md_cyan_500"
android:text="Label"
app:layout_constraintLeft_toLeftOf="@id/text"
app:layout_constraintRight_toRightOf="@id/text"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/text"
app:layout_constraintVertical_chainStyle="packed"/>
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/md_amber_500"
android:text="Text text"
app:layout_constraintWidth_max="wrap"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/label"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Is anybody there?
@camaelon I believe this is not fixed, could you look into this issue again, please?
ok let me check again
The problem still persists in version 2.1.0.
The problem still persists in version 2.1.1.