Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Textview with constrainedHeight inside a ConstraintLayout with height wrap_content expands the ConstraintLayout and prevents ellipsize #855

Open
DavidazAcc opened this issue May 30, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@DavidazAcc
Copy link

· Issue
I was trying to do a special view where a text can expand up it's constraints size and then ellipsize. This text has a button below itself, so the button should also be in the layout and prevent being hidden. The main layout should have wrap_content as there are more parts of the layout, and this button and text are constrained inside the bounds of another view.

The main problem is that the constraint layout is getting a little crazy if it has wrap_content as height, as the text is expanding the view when it shouldn't. If I had a fixed height in the parent (thing that I don't want), then the ellipsize of the TextView is not working. I attach two example xmls and their captures.

· Examples
Wrap Content issue:

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content">

    <View
        android:id="@+id/view_containing_text_and_button"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@android:color/darker_gray"/>

    <TextView
        android:id="@+id/text_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constrainedHeight="true"
        android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean mattis elit ac pulvinar sollicitudin. Integer at augue velit. Duis arcu erat, mattis vel euismod a, mollis eget est. Aenean congue sapien sit amet hendrerit vehicula. In vulputate neque non placerat dignissim. Proin sit amet mollis quam. "
        android:ellipsize="end"
        android:textStyle="bold"
        android:textSize="30sp"
        app:layout_constraintBottom_toTopOf="@id/button"
        app:layout_constraintEnd_toEndOf="@id/view_containing_text_and_button"
        app:layout_constraintStart_toStartOf="@id/view_containing_text_and_button"
        app:layout_constraintTop_toTopOf="@id/view_containing_text_and_button"
        app:layout_constraintVertical_chainStyle="packed" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="@id/view_containing_text_and_button"
        app:layout_constraintEnd_toEndOf="@id/view_containing_text_and_button"
        android:text="I'm a button!"
        app:layout_constraintStart_toStartOf="@id/view_containing_text_and_button"
        app:layout_constraintTop_toBottomOf="@id/text_view" />
    
    <!-- Here there are more things that can be added or not to the ConstraintLayout, thus the not fixed height in the ConstraintLayout -->

</androidx.constraintlayout.widget.ConstraintLayout>

Screenshot 2024-05-30 at 14 01 11

You can see in this example that the textview and button expands further from the view where they should be constrained (the grey view)

Not ellipsize issue // match parent or fixed size inside the ConstraintLayout

<?xml version="1.0" encoding="utf-8"?>
<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">

    <View
        android:id="@+id/view_containing_text_and_button"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@android:color/darker_gray"/>

    <TextView
        android:id="@+id/text_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constrainedHeight="true"
        android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean mattis elit ac pulvinar sollicitudin. Integer at augue velit. Duis arcu erat, mattis vel euismod a, mollis eget est. Aenean congue sapien sit amet hendrerit vehicula. In vulputate neque non placerat dignissim. Proin sit amet mollis quam. "
        android:ellipsize="end"
        android:textStyle="bold"
        android:textSize="30sp"
        app:layout_constraintBottom_toTopOf="@id/button"
        app:layout_constraintEnd_toEndOf="@id/view_containing_text_and_button"
        app:layout_constraintStart_toStartOf="@id/view_containing_text_and_button"
        app:layout_constraintTop_toTopOf="@id/view_containing_text_and_button"
        app:layout_constraintVertical_chainStyle="packed" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="@id/view_containing_text_and_button"
        app:layout_constraintEnd_toEndOf="@id/view_containing_text_and_button"
        android:text="I'm a button!"
        app:layout_constraintStart_toStartOf="@id/view_containing_text_and_button"
        app:layout_constraintTop_toBottomOf="@id/text_view" />

    <!-- Here there are more things that can be added or not to the ConstraintLayout, thus the not fixed height in the ConstraintLayout -->

</androidx.constraintlayout.widget.ConstraintLayout>

Screenshot 2024-05-30 at 14 06 07

Here we can see that when this works as intended design wise, but the ellipsize does not work and we can even see the text rendering behind the textview bounds.

I attach a picture of the render of the xml with a small text inside the view to show why this approach is needed:
Screenshot 2024-05-30 at 14 07 43

There you can see that we want both text and button together and centered inside the view and prevent it from expanding further with an ellipsize in that case.

· Version
I'm using ConstraintLayout version 'androidx.constraintlayout:constraintlayout:2.1.4'

@DavidazAcc DavidazAcc added the bug Something isn't working label May 30, 2024
@DavidazAcc DavidazAcc changed the title Textview with constrainedHeight inside a ConstraintLayout with height wrap content expands the ConstraintLayout and prevents ellipsize Textview with constrainedHeight inside a ConstraintLayout with height wrap_content expands the ConstraintLayout and prevents ellipsize May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant