Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TextView Text Truncate Behavior

In iOS text control has this behavior where if text doesn't fit on the screen it will get truncated and "..." gets automatically appended.

Any way to get similar behavior from Android TextView?

like image 639
AlexVPerl Avatar asked Oct 07 '14 14:10

AlexVPerl


1 Answers

Of course you also have this in Android.

The property is named "Ellipsize" and you have several options.

In XML:

android:ellipsize="start|marquee|end"

Or via code

textView.setEllipsize(TruncateAt.START | TruncateAt.END | TruncateAt.MARQUEE);

The values mean:

  • Start: Places the "..." at the text start
  • End: Places the "..." at the end
  • Marquee: Does a "scrolling marquee"

NOTES: Single Line

TextView should be single line, so to make it work, also do this (or their equivalent XML properties maxLines and singleLine):

textView.setSingleLine(true);

or

textView.setMaxLines(1);

Notes: Marquee Mode

For the Marquee to work, the TextView has to have focus (the marquee will start moving once you press the textview). You can also force the marquee to automatically scroll by issuing:

textView.setFocusable(true);
textView.requestFocus();
like image 104
rupps Avatar answered Nov 04 '22 04:11

rupps