7

How do you configure a TextView to truncate in the middle of a word?

So if I have text="Some Text" I want it to show as "Some Te" assuming the width supports that.

What instead I'm seeing is "Some"; its truncating the entire word "Text" even though there is plenty of space for a couple more characters.

Fraggle
  • 8,177
  • 5
  • 51
  • 85
  • Do you mind if there's a trailing ellipsis? Such as `Some T...` – Otra Jul 06 '11 at 18:11
  • No room for ellipsis, so yes I mind. I just want it to truncate. Tight on space. Might not be best design, but that's another matter. – Fraggle Jul 06 '11 at 18:16
  • Do you want it to cut the character at the end at the truncate mark, or do you only want it to show as many letters as it can support? For instance, if "Some Te" can be shown, but the "e" can only partially be shown, would you rather it show "Some T" or "Some Te" with the "e" being partially shown – Jason Robinson Jul 06 '11 at 18:33

3 Answers3

10

Here is what worked for me:

<TextView 
    android:layout_width="80px"
    android:layout_height="wrap_content"
    android:text="Some text"
    android:background="#88ff0000"
    android:singleLine="true"
    android:ellipsize="marquee"/>

enter image description here


Or with "..." at the end:

<TextView 
    android:layout_width="80px"
    android:layout_height="wrap_content"
    android:text="Some text"
    android:background="#88ff0000"
    android:singleLine="true"
    />

enter image description here


While using android:lines doesn't work:

<TextView 
    android:layout_width="80px"
    android:layout_height="wrap_content"
    android:text="Some text"
    android:background="#88ff0000"
    android:lines="1"
    android:ellipsize="marquee"/>

enter image description here

This is most likely a bug and I believe I've seen some bug report about that.

inazaruk
  • 72,103
  • 23
  • 181
  • 156
  • Ok, thanks that works. Although I'm also trying the same as 1st xml above, but with android:scrollHorizontally="true" (credit Otra below for putting me on that track), so as to avoid the deprecated singleLine attribute. Seems to also work. – Fraggle Jul 06 '11 at 18:33
2

Here is my code for a TextView that "truncates" the word with no ellipsis. It doesn't quite cut it off, it simply lets it run off the edge, giving the slight impression that it's been truncated.

<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:singleLine="true"
    android:scrollHorizontally="false"
    android:ellipsize="none"
    android:text="@string/hello"
/>
Otra
  • 7,898
  • 3
  • 31
  • 48
0
Found Two ways to Truncating texview.
Text to truncate : "Hello World! sample_text_truncate"
Case 1:
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"`enter code here`
        android:text="Hello World! sample_text_truncate"
        android:textSize="28sp"
        android:ellipsize="end"
        android:singleLine="true"/>

Result: Hello World! sample_text_trunc...

Case 2:
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World! sample_text_truncate"
        android:textSize="28sp"
        android:ellipsize="end"
        android:maxLines="1"/>

Result: Hello World!...

So i found difference in singleline and maxlines properties.Unfortunately singleline in depricated.I wanted singleline result.
RKB
  • 41
  • 4