140

If a String is longer than the TextView's width it automatically wraps onto the next line. I can avoid this by using android:singleLine (deprecated) or by setting android:inputType="text". What I need now is something that replaces the last 3 characters of my String with "...". Since I'm not using a monospace font this will always be different depending on the letters used in my String. So I'm wondering what's the best way to get the last 3 characters of a String in a TextView and replace them. Maybe there's already something implemented in the Android framework, since this must be a common problem.

BC2
  • 892
  • 1
  • 7
  • 23
znq
  • 42,465
  • 38
  • 114
  • 143

3 Answers3

307

You should be able to use the "ellipsize" property of a text view:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text_mytext"
    android:ellipsize="end"
    android:maxLines="1"
/>

You may also need to apply gravity values to the layout too; I have sometimes seen "auto-stretching" views without them.

Mateus Gondim
  • 4,226
  • 4
  • 26
  • 48
Nate
  • 4,474
  • 1
  • 18
  • 9
  • Perfect. Thanks. I have tried that before, but didn't work, because I used android:inputType="text" instead of android:singleLine="true" (which is supposed to be deprecated). But now I'm gonna stick with android:singleLine="true". Muchas gracias. – znq Nov 03 '09 at 14:08
  • 44
    It still doesn't add the "...". For me, this question is not fully answered! This answer will make "short verylongword" to just "short" and not "short verylon..." On top of that you guys suggest using a deprecated attribute. – OneWorld Sep 22 '10 at 12:06
  • 8
    Works great, however singleLine is deprecated so you must use maxLines="1" instead. Side note, ellipsize works with multiple lines as well! – Torre Lasley Oct 01 '16 at 16:13
  • 4
    singleLine is deprecated. Use maxLines="1". just a comment to old answer – Ewoks Nov 21 '16 at 11:09
  • 1
    MaxLine=1 will truncate the textview in the middle. SingleLine=True works as expected. – Snake Apr 21 '18 at 11:12
  • @torre-lasley Using maxLines="1" and ellipsize doesn't work properly with API lower than 22 (tested on API 21 and API 16). For example this text `i have a abcdefghijklmhopqrstuvw` it should show as `i have a abcdefgh...` but instead it's showing as `i have a ...` – HeyThere Dec 11 '18 at 01:38
  • Using ConstraintLayout, I needed to use constrainedWidth="true" as well – Hocine B Nov 17 '19 at 21:32
  • maxLines="1" doesn't give the same result as singleLine="true" – Hasan El-Hefnawy Mar 09 '20 at 11:06
33

Found an interesting work-a-round for this problem.

maxLines=1
ellipsize=end
scrollHorizontally=true

The trick is that last statement about horizontal scrolling .... check it out. It at least works on v2.2.

BonanzaDriver
  • 5,952
  • 5
  • 30
  • 34
5

Programmatically, you can use:

TextView tx = new TextView(this);
tx.setTextSize(13);
tx.setGravity(Gravity.CENTER);
tx.setTop(90);
tx.setText("Long text here");
tx.setTextColor(Color.BLACK);
tx.setSingleLine(true);
tx.setEllipsize(TruncateAt.END);
Pang
  • 8,605
  • 144
  • 77
  • 113
xevser
  • 319
  • 4
  • 5