0

enter image description here

  • If Text1 is longer, than it should push everything to the right
  • If Text2 is longer, than it should push everything to the left
  • Otherwise, the arrow should be centered according to parent view
  • If any TextView exceeds, thus the whole view exceeds the parent view, then it should be trimmed (ellipsize)
Ron D.
  • 3,774
  • 6
  • 31
  • 39

1 Answers1

1

You can achieve this using ConstraintLayout. Create a packed horizontal chain, and use wrap_content plus constrainedWidth on each text view:

<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="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:ellipsize="end"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/image"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constrainedWidth="true" />

    <TextView
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:ellipsize="end"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@id/image"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constrainedWidth="true" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@id/left"
        app:layout_constraintEnd_toStartOf="@id/right"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

enter image description here

enter image description here

If both text views have long text, you can control which one gets given space first by re-ordering the tags within the ConstraintLayout. This won't change how they're positioned; it will change only whether the left or right text stretches first.

Ben P.
  • 52,661
  • 6
  • 95
  • 123