liminal
liminal

Reputation: 1164

Constraint Layout buttons wrap text with weight

I have a ConstraintLayout container with 2 text buttons. Both setting android:ellipsize="end" and android:maxLines="1".

The diagrams below describe what I am trying to accomplish. If the text of the first button is long, it should take up to 60% of the space and the second button can take up the rest if necessary.

buttons

Upvotes: 0

Views: 436

Answers (2)

liminal
liminal

Reputation: 1164

Using a Guideline was the answer:

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="1"
            app:layout_constrainedWidth="true"
            app:layout_constraintEnd_toStartOf="@id/actionButtonEndGuideline"
            app:layout_constraintHorizontal_bias="0.001"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintWidth_default="wrap"
            tools:text="Button 1 text" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/actionButtonEndGuideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.6" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="1"
            app:layout_constrainedWidth="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0"
            app:layout_constraintStart_toEndOf="@id/button1"
            app:layout_constraintTop_toTopOf="@id/button1"
            app:layout_constraintWidth_default="wrap"
            tools:text="Button 2 text" />

    </androidx.constraintlayout.widget.ConstraintLayout>

Note the app:layout_constraintHorizontal_bias="0.001" on the first button. I spent quite a bit of time trying to get this to work with app:layout_constraintHorizontal_bias="0" until I ran into this answer layout_constrainedWidth not working properly.

This is a bug in the latest stable release of constraintlayout dependency at the time of writing this (1.1.3) and apparently the fix is available in beta (2.0.0-beta8)

Upvotes: 0

elbert rivas
elbert rivas

Reputation: 1464

Not a direct answer but this might help you.

This can be achieved by PercentRelativeLayout however it is deprecated already. Fortunately, it can be replicated using ConstraintLayout using the following.

app:layout_constraintGuide_percent=".15"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"

For detailed guide, check this documentation

Upvotes: 1

Related Questions