1

I have a widget where I need to display some text on one row. If the text does not fit, I would like to show as much as possible and end the text with "..." to show that not all text is displayed. Is there a way to discover how long the displayed text will be before rendering the widget, so that I can replace the last part of the text with "..."?

Cheers,

pgsandstrom
  • 14,361
  • 13
  • 70
  • 104
  • 1
    I believe the answer to this question is exactly what you're looking for: http://stackoverflow.com/questions/1666736/android-textview-automatically-truncate-and-replace-last-3-char-of-string :) – Julian Jan 13 '11 at 10:30
  • 1
    similar question: http://stackoverflow.com/questions/3257293/measuring-text-width-to-be-drawn-on-canvas-android – bigstones Jan 13 '11 at 10:30

1 Answers1

3

You don't need to do that - a TextView can do it for you.

myTextView.setSingleLine(true);
myTextView.setEllipsize(TextView.TruncateAt.END);
Theasis
  • 91
  • 1
  • 4
  • Damnit, it does not seem to work on widgets in pre-eclair phones. Are there any known workarounds? ;_; – pgsandstrom Jan 13 '11 at 11:50
  • I just tried it on Donut and it is doing what I expect. Are you sure there's not something else involved? – Theasis Jan 13 '11 at 12:31