2

I've been trying to obtain a certain layout but all my attempts have failed short.

I have 2 textviews within a limited space. As long as there is space to show both, I want them to show one after the other:

| T1 T2222          |
| T111111 T2222     |

But when space runs short, I want only the first one to get ellipsized or otherwise cut, such as this:

| T1111111... T2222 |

It may help - or not - that both are only supposed to have 1 line.

It may help - or not - that the second's width can be fixed.

  • I've tried the ideas at Two TextViews side by side, only one to ellipsize? but that case is different from mine because their T2 is meant to anchor at the right, whereas I'd like to have T2 just follow T1.

  • I've tried specifying a minWidth for T2, but it doesn't seem to be honoured; plain width is, but I can't seem to control T1's behaviour.

  • I've dabbled with weights, but found nothing that would solve the problem. But that may be my inexperience.

  • I could specify a maxWidth for T1, and it it were honoured it might do what I wish, but the problem is that the whole width of the limited space is unspecified.

At this point I'm starting to think there is no way to do it - unless maybe programatically, which is a route I'd like to avoid.

Thank you for any suggestions.

Community
  • 1
  • 1
entonio
  • 2,143
  • 1
  • 17
  • 27

1 Answers1

6

Would concatenating the two Strings and putting the result into a single TextView with ellipsize="middle" set work?

Or a layout, where the second textview is aligned to the right?

| T1          T2222 |
| T111111     T2222 |
| T1111111... T2222 |

Update
Taking advantage of the ability that the second text view's width can be fixed, the RelativeLayout below serves with the desired layout (by explicitly setting the second view's width to 60dp):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:gravity="fill">
    <TextView android:id="@+id/secondText" 
        android:layout_width="60dp" android:layout_height="wrap_content"
        android:singleLine="true" android:layout_alignParentRight="true" />
    <TextView android:id="@+id/dynamicText" 
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_marginRight="5dp" android:layout_toLeftOf="@id/secondText"
        android:singleLine="true" android:ellipsize="end"  />   
</RelativeLayout>

The code above generates the following layout:

| T1 T2222          |
| T111111 T2222     |
| T1111111... T2222 |

width an 5dp gap between the two TextViews.

rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • 1. I hadn't thought of this, it's an interesting idea, though not quite what the designers had in mind. Thanks. 2. No, T2 must be next to T1. – entonio May 02 '11 at 19:43
  • Please take a look at the layout in my update, I hope it will work for you. – rekaszeru May 02 '11 at 20:42
  • Thanks a million! :) I'd given up on this, but your example works perfectly. I'd vote the answer up, but I've just registered today... Just for the record: without gravity="fill", the RelativeLayout would only fill from the right; and if T2 isn't declared first, T2 will be ellipsed instead of T1. – entonio May 02 '11 at 21:27
  • I'm glad, you'made it, and even more happy for the challenge not being satisfied with my knowledge you made me learn. Thank you! – rekaszeru May 03 '11 at 05:00