10

I have a TextView. I want it to ellipsize if longer than its available width. This does not work unless the input string has no spaces... can anyone provide an example of this working? I've tried different combinations of:

singleLine="true" maxLines="1" scrollHorizontally="false"

none of these have any effect. Again, if I supply a string that has no spaces in it, then the ellipsis appears correctly. What am I missing? I've tried this on 1.5, 1.6, 2.0, all same problem.

Thanks

Mark
  • 39,551
  • 15
  • 41
  • 47

4 Answers4

12

Ellipsize is broken (go vote on the bug report, especially since they claim it's not reproducible) so you have to use a minor hack. Use:

android:inputType="text"
android:maxLines="1"

on anything you want to ellipsize. Also, don't use singleLine, it's been deprecated since 1.5.

Jeremy Logan
  • 47,151
  • 38
  • 123
  • 143
  • 1
    @fiXedd: except that your method doesn't work - as per your link and BGS's comment, what works for me is comment #9: Try to use android:lines="1" and android:scrollHorizontally="true" instead of android:singleLine="true"_ – Richard Le Mesurier Nov 04 '11 at 07:30
  • For TextView's, `android:ellipsize="end"` combined with `android:maxLines="1"` works great ! – Someone Somewhere Sep 10 '13 at 19:09
  • This way I get spell-check on textView's. Which looks strange and makes my big ass listview laggy. So it left me no other choise than using `android:singleLine="true"`. – Roel May 04 '15 at 15:19
11

this was the only combination that I could get to work on SDK ver4:

android:ellipsize="end"
android:singleLine="true"

(Yes I know it says it's deprecated but I'm left without a choice)

Ross Hambrick
  • 5,880
  • 2
  • 43
  • 34
2

Try with:

textView.setSingleLine();
TruncateAt truncate = TruncateAt.END;
textView.setEllipsize(truncate);
Zeeshan
  • 1,625
  • 17
  • 25
jgimbert
  • 21
  • 1
0

Simple Solution.

int limit = 9;
if (str.length() > limit) {
    textView.setText(str.substring(0, limit)+"...");
} else {
    textView.setText(str);
}
coderek
  • 1,860
  • 14
  • 20