Search code examples
androidandroid-layoutandroid-constraintlayout

Constraint layout wrap textview but constraint width when its too long to let other views be visible


I have to design this layout. Icon should stick to the textview.

enter image description here

This is what I've tried which looks exactly what is needed.

<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingStart="@dimen/padding_m"
        android:paddingTop="@dimen/padding_s"
        android:paddingEnd="@dimen/padding_m"
        android:paddingBottom="@dimen/padding_s"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/title_container"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/more_options"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/active_group_name"
                style="@style/AppbarTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:maxLines="1"
                app:layout_constrainedWidth="true"
                tools:text="Ranjan Malav" />

            <FrameLayout
                android:id="@+id/account_option_icon_container"
                android:layout_width="36dp"
                android:layout_height="36dp"
                android:layout_gravity="center"
                android:layout_marginStart="@dimen/margin_xs"
                android:background="@drawable/circle_background_36dp">

                <ImageView
                    android:id="@+id/account_option_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    app:srcCompat="@drawable/ic_outline_settings_24" />
            </FrameLayout>

        </LinearLayout>

        <ImageView
            android:id="@+id/more_options"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_baseline_more_vert_24"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

But when the text is long, setting icon is getting hidden. enter image description here

How can I give icon priority and let it take the space and then textview should wrap itself in the remaining space?


Solution

  • This works as intended. This answer on strackoverflow was helpful https://stackoverflow.com/a/44412219/6168272.

    <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingStart="@dimen/padding_m"
            android:paddingTop="@dimen/padding_s"
            android:paddingEnd="@dimen/padding_m"
            android:paddingBottom="@dimen/padding_s"
            app:layout_constraintTop_toTopOf="parent">
    
            <TextView
                android:id="@+id/active_group_name"
                style="@style/AppbarTitle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:maxLines="1"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintHorizontal_chainStyle="packed"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@id/account_option_icon_container"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintWidth_default="wrap"
                tools:text="Ranjan Ranjan Ranjan Ranjan Ranjan" />
    
            <FrameLayout
                android:id="@+id/account_option_icon_container"
                android:layout_width="36dp"
                android:layout_height="36dp"
                android:layout_gravity="center"
                android:layout_marginStart="@dimen/margin_xs"
                android:background="@drawable/circle_background_36dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@id/more_options"
                app:layout_constraintStart_toEndOf="@id/active_group_name"
                app:layout_constraintTop_toTopOf="parent">
    
                <ImageView
                    android:id="@+id/account_option_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    app:srcCompat="@drawable/ic_outline_settings_24" />
            </FrameLayout>
    
            <ImageView
                android:id="@+id/more_options"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_baseline_more_vert_24"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>