saintjab
saintjab

Reputation: 1642

ConstraintLayout set constraints on views to grow but not touch

I have three views in a constraint layout A (textview), B(textview) and C (imageview). B have dynamic text and hence I want it to grow as much as possible but without touching C. It can have multiple lines but I don't want to fix its width and shouldn't make C off screen. How can I use constraint layout for such layout. This is what I want to achieve https://i.sstatic.net/EIbnz.png If the text is more, make it multiline. This is what I get https://i.sstatic.net/VG7Z5.png .The last image view is pushed out of screen if text is long.

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@drawable/button_background_rounded_gray"
    android:id="@+id/container"
    android:layout_width="wrap_content"
    android:layout_height="30dp">

<androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/docIcon"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:textAllCaps="true"
        android:gravity="center"
        android:text="PDF"
        android:textColor="@color/tcWhite"
        android:background="@drawable/button_background_rounded_red"
        android:layout_marginStart="10dp"
        android:textSize="7sp"
        android:layout_width="20dp"
        android:layout_height="20dp"/>

<androidx.appcompat.widget.AppCompatTextView
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:textSize="12sp"
        android:id="@+id/docName"
        tools:text="Accommodation Voucher -  Circus at Disneyland Paris - Demo - 11 Oct.pdf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintStart_toEndOf="@+id/docIcon"
        android:layout_gravity="center_vertical"/>

<androidx.appcompat.widget.AppCompatImageView
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/docStatus"
        app:srcCompat="@drawable/ic_picture_as_pdf_red_24dp"
        android:layout_marginEnd="10dp"
        android:layout_width="20dp"
        android:layout_height="20dp"
        app:layout_constraintStart_toEndOf="@+id/docName"/>

Upvotes: 2

Views: 1330

Answers (2)

Cheticamp
Cheticamp

Reputation: 62841

You don't show how the overall width for ConstraintLayout that you posted is determined. For this answer, I use match_parent but another constraint will work as well.

I key to allowing the text to expand to the constraints but still honor the constraints is to set app:layout_constrainedWidth="true" on the TextView. I have also placed the three views into a horizontal chain. See the ConstraintLayout Developer Guide.

WRAP_CONTENT : enforcing constraints (Added in 1.1)

If a dimension is set to WRAP_CONTENT, in versions before 1.1 they will be treated as a literal dimension -- meaning, constraints will not limit the resulting dimension. While in general this is enough (and faster), in some situations, you might want to use WRAP_CONTENT, yet keep enforcing constraints to limit the resulting dimension. In that case, you can add one of the corresponding attribute:

   app:layout_constrainedWidth=”true|false”
   app:layout_constrainedHeight=”true|false”

activity_main.xml

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/docIcon"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginStart="10dp"
        android:background="@drawable/ic_launcher_background"
        android:gravity="center"
        android:text="PDF"
        android:textColor="@android:color/white"
        android:textSize="7sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/docName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:textAllCaps="true" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/docName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="This is a very long name. This is a very long name. This is a very long name. "
        android:textSize="12sp"
        app:layout_constrainedWidth="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/docStatus"
        app:layout_constraintStart_toEndOf="@+id/docIcon"
        app:layout_constraintTop_toTopOf="parent" />
    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/docStatus"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginEnd="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/docName"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_launcher_foreground" />
</androidx.constraintlayout.widget.ConstraintLayout>

Here is a simpler demonstration.

enter image description here

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="400dp"
    android:layout_height="wrap_content"
    android:background="@android:color/darker_gray">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Type here..."
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toStartOf="@+id/imageView"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher" />
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 5

Amine
Amine

Reputation: 2434

Add those two properties to the second AppCompactTextView

app:layout_constraintRight_toRightOf="@id/docStatus"
app:layout_constraintHorizontal_bias="1"

And make the width of that textView to 0dp.

Upvotes: 0

Related Questions