Android Layout: How to ellipsize TextView depending on surrounding textviews?

569 Views Asked by At

I need your help designing a layout that works with the few following requirements:

example layout

Requirements:

  • TextA (Green):

    1. is always there.
    2. may vary in length (1 to 50 chars).
    3. is the only one which must be ellipsized when content is too long for the space.
  • TextB (Yellow):

    1. can be empty. (width: 0dp)
    2. may vary in length (0 to 10 chars).
    3. must be fully visible (not ellipsized/truncated) if not empty
    4. must stick to the right side of the TextA.
  • TextC (Red):

    1. is always there.
    2. has a fixed width (ie: 100dp).
    3. is always sticking to the right in the parent.

Note: TextA, TextB & TextC are TextViews.

My question is similar to this question but with 3 parts instead of 2 and the requirements are a little different.

Anyone has an idea how to achieve that?

Thank you

2

There are 2 best solutions below

7
On BEST ANSWER

You cannot accomplish this 100% in xml as you will need to conditionally set the visibility on TextB to View.GONE. However, with a ConstraintLayout you can accomplish the rest of the behavior like this: layout preview

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/TextA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:text="TextAaekfnvauefoviqwejnoiwjeofijnweoimivejmgpiwvmpgr"
    android:ellipsize="end"
    android:maxLines="1"
    android:textColor="@color/green"
    app:layout_constraintEnd_toStartOf="@+id/TextC"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/TextC"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:text="TextC"
    android:textColor="@color/red"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/TextB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:text="TextB"
    android:textColor="@color/yellow"
    app:layout_constraintEnd_toStartOf="@+id/TextC"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toEndOf="@+id/TextA"
    app:layout_constraintTop_toTopOf="parent" />

3
On

Your question is answered here.

But the solution will make your textviews have same width. Remember to use android:layout_weight="1" just for the middle textview.