11

I added

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

to my TextView with the intention of showing three dots at the end and max text limit of 8 but it's not working. It shows neither the dots nor any text limit.

arcyqwerty
  • 10,325
  • 4
  • 47
  • 84
Android Developer
  • 9,157
  • 18
  • 82
  • 139

5 Answers5

20

Both of your attributes for adding 'the three dots effect' are correct

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

but the effect happens only when the TextView does not have any free space on the screen to show the text.

To set the limit of the text in TextView you can use

android:maxLength="8"

but it doesn't add the three dots, and as far as I am aware you would have to do it manually.

Something like this

String text = "A bit longer text";
if (text.length() > 8) {
    text = text.substring(0, 8) + "...";
}

The android:maxEms is something else than previous, you can read more about it

What is meant by Ems? (Android TextView)

Community
  • 1
  • 1
Marko
  • 20,385
  • 13
  • 48
  • 64
8

ellipsize will not work with width wrap_content

You can directly try the following example for testing.

<TextView
    android:layout_width="30dp"
    android:text="jhakjshdkajdhkjashkjdhakjsdjas"
    android:maxLines="1"
    android:lines="1"
    android:singleLine="true"
    android:ellipsize="end"
    android:layout_height="wrap_content" />

I hope this will help you to resolve the issue.

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
Ajay Kumar Meher
  • 1,932
  • 16
  • 24
3

For some reason, I accidentally had put

android:inputType="textCapSentences"

on my TextView which prevented my ellipses from showing

nilsi
  • 10,351
  • 10
  • 67
  • 79
  • 1
    Yes its worked for me. After 4 hours of searching.. – Abreeth Nov 02 '21 at 09:15
  • Nice catch man! I Changed every attribute related to the line settings and none worked. As per your answer deleted the `inputType` attribute and daamn finally the ellipsize appeared! – Kozmotronik Nov 30 '21 at 14:59
1

set android:layout_width="0dp" instead of android:layout_width="wrap_content" worked for me

Kirguduck
  • 748
  • 1
  • 9
  • 20
0

Best Wat to use

 <androidx.appcompat.widget.AppCompatTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:lines="1"
        android:maxLines="1"
        android:minEms="1"
        android:singleLine="true" />
Peter
  • 587
  • 6
  • 16