Search code examples
androidandroid-constraintlayoutconstraint-layout-chains

How do I center a packed chain in a ConstraintLayout that contains a View with width `0dp`?


I want to center the headline of my page. The headline can look like (1) Dokument1 or (3) GreatDocument. I want to have right and left margins of 60dp and the name of the document has a variable length. When I use wrap_content for @+id/title the centering works well, however the margins don't work for long names of documents. When I use 0dp the margins get respected but the centering doesn't work.

How do I get the packed chain in my ConstraintLayout to center while having a dynamic length and having margins.

<android.support.constraint.ConstraintLayout
    android:id="@+id/top_bar_xml"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/open_brackets"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="("
        android:gravity="center_vertical"
        android:layout_marginStart="60dp"

        android:textSize="20sp"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintEnd_toStartOf="@id/page_count"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/page_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:gravity="center_vertical"
        android:textSize="20sp"
        app:layout_constraintStart_toEndOf="@id/open_brackets"
        app:layout_constraintEnd_toStartOf="@id/close_brackets"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/close_brackets"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=") "
        android:gravity="center_vertical"
        android:textColor="@color/black"
        android:textSize="20sp"
        app:layout_constraintStart_toEndOf="@id/page_count"
        app:layout_constraintEnd_toStartOf="@id/title"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="60dp"
        android:text="DocumentTitle"
        android:textSize="20sp"
        android:gravity="center_vertical"
        android:ellipsize="end"
        app:layout_constraintStart_toEndOf="@+id/close_brackets"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

(I simplified the example by removing references to ViewModels for page count and title; the document has additional Views below that have a lower margin)


Solution

  • You can set width of @id/title to wrap_content and add

    app:layout_constrainedWidth="true"
    

    attribute to it so that the constraints of that TextView are respected when it gets too big to satisfy the margins.