10

I've defined the following TextView:

    <TextView
    android:id="@+id/textview_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:layout_alignParentTop="true"
    android:layout_marginRight="25dip"
    android:ellipsize="end"/>

And as expected the text is cut off if it's too long and replaced by "...". But at the end of the three points appears a question mark surrounded by a square.

How can I get rid off this question mark?

Thanks!

GabrielWeis
  • 342
  • 1
  • 3
  • 13

2 Answers2

35

To quote myself from one of my books:

Android's TextView class has the built-in ability to "ellipsize" text, truncating it and adding an ellipsis if the text is longer than the available space. You can use this via the android:ellipsize attribute, for example. This works fairly well, at least for single-line text.

The ellipsis that Android uses is not three periods. Rather it uses an actual ellipsis character, where the three dots are contained in a single glyph. Hence, any font that you use that you also use the "ellipsizing" feature will need the ellipsis glyph.

Beyond that, though, Android pads out the string that gets rendered on- screen, such that the length (in characters) is the same before and after "ellipsizing". To make this work, Android replaces one character with the ellipsis, and replaces all other removed characters with the Unicode character 'ZERO WIDTH NO-BREAK SPACE' (U+FEFF). This means the "extra" characters after the ellipsis do not take up any visible space on screen, yet they can be part of the string.

However, this means any custom fonts you use for TextView widgets that you use with android:ellipsize must also support this special Unicode character. Not all fonts do, and you will get artifacts in the on-screen representation of your shortened strings if your font lacks this character (e.g., rogue X's appear at the end of the line).

CommonsWare
  • 954,112
  • 185
  • 2,315
  • 2,367
  • Oh man, I should have read the entire book ;-) Thank you for that tip! – GabrielWeis Apr 27 '11 at 23:21
  • 1
    @GabrielWeis: Hah! I elected to do the quote, simply because it's a pain to explain, and I didn't feel like writing it up again. Assuming you have rights to fuss with your font file, somebody with a font editor (and the expertise to use one well) should be able to add this glyph. – CommonsWare Apr 27 '11 at 23:24
  • @CommonsWare: Thanks for the explanation, but in my case I see these artifacts on a HTC Hero (2.1) but I don't on a Nexus One (2.2 or above) nor on a Samsung Galaxy S (2.2 or above). If this is a font related problem, why is that? – Xavi Gil Jul 31 '11 at 14:01
  • 2
    @Xavi: Perhaps the Hero uses a different font. Or perhaps they tinkered with the standard font and removed this glyph. – CommonsWare Jul 31 '11 at 14:06
9

Had the same problem. Somebody with FontForge knowledge fixed the U+FEFF in my custom fonts and now they work well. Here is how he described what he did:

"I'm not good with Fontforge, so I cheated. Basically, opened the font in Fontforge, selected the space character, ctrl+c for copy, then selected FEFF, ctrl+v for paste. Then menu Metrics->Set Width and set to zero. Then File->Generate Fonts."

user1139880
  • 1,818
  • 3
  • 18
  • 27