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.
Asked
Active
Viewed 1e+01k times
149
4 Answers
328
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
- 5,362
- 6
- 31
- 51

Nate
- 4,724
- 1
- 21
- 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
-
45It 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
-
9Works 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
-
4singleLine is deprecated. Use maxLines="1". just a comment to old answer – Ewoks Nov 21 '16 at 11:09
-
1MaxLine=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
35
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
- 6,411
- 5
- 32
- 35
0
Just add
android:ellipsize="end"
on XML
and your code should look like this
<TextView
android:id="@+id/newsTitleTextView"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/newsThumbnailImageView"
android:ellipsize="end"
android:maxLines="2"
android:text="Analysts See Slower Bitcoin Hashrate Growth in 2022 Amid Market Correction "
android:textSize="15sp" />

Reny
- 19
- 5