Don Madrino
Don Madrino

Reputation: 147

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

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

example layout

Requirements:

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

Upvotes: 0

Views: 791

Answers (2)

Frank Egan
Frank Egan

Reputation: 26

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" />

Upvotes: 1

mohammad fakhraee
mohammad fakhraee

Reputation: 322

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.

Upvotes: 0

Related Questions