0

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

Don Madrino
  • 147
  • 1
  • 11

2 Answers2

1

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

Frank Egan
  • 26
  • 3
  • Interesting... :-) I could use a DataBinding to set a conditionnal visibility for the TextB. Do you think it'd work? – Don Madrino Dec 10 '19 at 16:12
  • I'm not familiar with the data binding library but it seems possible – Frank Egan Dec 10 '19 at 16:26
  • I just tried you sample code and it dosent fully work (even with the TextB visibility to "Gone"). No text is getting ellipsized and the TextA ends up over the TextC. :-( Could you tell me what is the "textView6" reffering to? And if it's not too much to ask, could you make me a fully(except for the TextB visibility issue) functionnal layout example please? :-) – Don Madrino Dec 11 '19 at 02:03
  • @IceMatter Sorry I must have missed one of the ids while copy the code over. It's updated now – Frank Egan Dec 11 '19 at 17:43
  • Thanx for the update but it still dosen't work with a long TextA; it overlapps the TextC... :-( – Don Madrino Dec 12 '19 at 00:58
  • Did you try playing around with latout-width="match_constraint" on TextA? – Frank Egan Dec 12 '19 at 19:56
  • I added app:layout_constrainedWidth="true" to TextA and TextC and it seems to work now! :-D thank you! – Don Madrino Dec 12 '19 at 22:51
0

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.

  • Thank you, but this does not seem to be the solution I'm looking for... I need the width of TextA & TextB to be flexible (ie: wrap_content) and the width of TextC to be fixed @ 100dp. – Don Madrino Dec 10 '19 at 15:44
  • When you set `android:layout_weight="1"`, the last textview will stick in the end and if the middle textview reaches the end, it will return to ellipsize mode – mohammad fakhraee Dec 10 '19 at 15:48
  • If I get you right, TextB (or TextC) would become ellipsized at some point? Cause I only need the TextA to be ellipsized; the two other ones must never be. – Don Madrino Dec 10 '19 at 15:53