constraintlayout icon indicating copy to clipboard operation
constraintlayout copied to clipboard

layout_constraintWidth_max ignored when layout_constraintWidth_percent also specified

Open devconsole opened this issue 4 years ago • 11 comments

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.

devconsole avatar Dec 14 '20 17:12 devconsole

Which version of 2.0 are you using? trying to replicate it it seems to work. Could you share a full layout example? Thanks

camaelon avatar Dec 17 '20 07:12 camaelon

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.

devconsole avatar Dec 17 '20 11:12 devconsole

Ah great -- thanks for the update, I can reproduce. Let me look into it.

camaelon avatar Dec 17 '20 18:12 camaelon

Fixed in tip of tree

camaelon avatar Dec 17 '20 21:12 camaelon

When will this fix be released? I have tested with 2.1.0-beta02 and it still does not work.

devconsole avatar May 19 '21 19:05 devconsole

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>

devconsole avatar May 19 '21 20:05 devconsole

Is anybody there?

devconsole avatar May 26 '21 11:05 devconsole

@camaelon I believe this is not fixed, could you look into this issue again, please?

devconsole avatar Jun 09 '21 13:06 devconsole

ok let me check again

camaelon avatar Jun 09 '21 15:06 camaelon

The problem still persists in version 2.1.0.

devconsole avatar Aug 16 '21 14:08 devconsole

The problem still persists in version 2.1.1.

devconsole avatar Oct 12 '21 12:10 devconsole