android:ellipsize="end" and android:maxEms not working

17,541

Solution 1

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)

Solution 2

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.

Solution 3

For some reason, I accidentally had put

android:inputType="textCapSentences"

on my TextView which prevented my ellipses from showing

Share:
17,541
Android Developer
Author by

Android Developer

Updated on June 07, 2022

Comments

  • Android Developer
    Android Developer almost 2 years

    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.

  • Android Developer
    Android Developer over 8 years
    i want to show three dots in the end.. read this.. stackoverflow.com/questions/12686468/…
  • Android Developer
    Android Developer over 8 years
    why is android:ellipsize="end" not working?it should show three dots in case of large text length?
  • Marko
    Marko over 8 years
    How do you want to limit the text? It usualy works if your width is match_parent and you have more text than it can into one line.
  • Android Developer
    Android Developer over 8 years
    I want to limit text to maximum 8 characters and if more than that are there then it should show three dots..
  • stallianz
    stallianz over 8 years
    try android:maxLength="8"
  • Marko
    Marko over 8 years
    ellipsize will work with layout_width set to wrap_content aswell, but it has to take all the available space that it has assigned on the screen.
  • Android Developer
    Android Developer over 8 years
    @Marko Reducing android:maxEms="8" value to android:maxEms="4" also solved my issue..
  • Marko
    Marko over 8 years
    @AndroidLearner this is not the right approach. maxEms means how many 'M' charachters can fit in your TextView. You should avoid using it, since it is wrong. You can get a better explanation from the link in my answer.
  • blackHawk
    blackHawk almost 5 years
    this should be best answer
  • Abreeth
    Abreeth over 2 years
    Yes its worked for me. After 4 hours of searching..
  • Kozmotronik
    Kozmotronik over 2 years
    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!