Rafael
Rafael

Reputation: 6369

Android right aligned view restricted by view on the left

I want to make layout with dynamic text like on the picture. The nearest thing I came up is this code: I still have problems like hidden text and icon by parent ConstraintLayout padding. Is there better solution to make such dynamic layout.

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/addressWrapper"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingEnd="12dp"
    android:paddingStart="12dp"
    android:paddingTop="2dp"
    android:paddingBottom="3dp"
    android:background="@drawable/address_background_themed"
    app:layout_constraintHorizontal_bias="1"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    >
    <ImageView
        android:id="@+id/iconImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:src="@drawable/round_person_18px"
        android:tint="@color/grey"
        app:layout_constraintWidth_default="wrap"
        app:layout_constraintEnd_toStartOf="@+id/txtAddress"
        app:layout_constraintTop_toTopOf="@+id/txtAddress"
        app:layout_constraintBottom_toBottomOf="@+id/txtAddress"
        />

    <TextView
        android:id="@+id/txtAddress"
        style="@style/Body1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="middle"
        android:includeFontPadding="false"
        android:singleLine="true"
        android:textAlignment="textStart"
        android:textColor="?TransactionDetailValueTextColor"
        app:layout_constraintWidth_default="wrap"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        tools:text="rgergrgergergrververewfewfweenknknknknkgk8f7xxau"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

Upvotes: 0

Views: 74

Answers (1)

Pawel Laskowski
Pawel Laskowski

Reputation: 6346

Your wrapper is wrapping content, so it's best to provide all constraints for the Views inside of it. Now, app:layout_constraintWidth_default is deprecated and for Views that are wrap_content app:layout_constrainedWidth should be used instead.

<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/addressWrapper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingEnd="12dp"
        android:paddingStart="12dp"
        android:paddingTop="2dp"
        android:paddingBottom="3dp"
        android:background="@drawable/address_background_themed"
        app:layout_constraintHorizontal_bias="1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <ImageView
            android:id="@+id/iconImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:src="@drawable/round_person_18px"
            android:tint="@color/grey"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/txtAddress"
            app:layout_constraintTop_toTopOf="@+id/txtAddress"
            app:layout_constraintBottom_toBottomOf="@+id/txtAddress" />

        <TextView
            android:id="@+id/txtAddress"
            style="@style/Body1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="middle"
            android:includeFontPadding="false"
            android:singleLine="true"
            android:textAlignment="textStart"
            android:textColor="?TransactionDetailValueTextColor"
            app:layout_constrainedWidth="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/iconImage"
            tools:text="rgergrgergergrververewfewfweenknknknknkgk8f7xxau"
            android:layout_marginBottom="8dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 1

Related Questions