Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show ellipses on my TextView if it is greater than the 1 line?

People also ask

How do you get 3 dots at the end of a TextView text?

You are applying to your TextView a compound Drawable on the right.. to make the three dots appear in this scenario, you have to apply a android:drawablePadding="{something}dp" attribute to the TextView as well. Hope it helps!

How do you check if a TextView's text exceeds the max lines?

text_view. post(new Runnable() { @Override public void run() { if (text_view. getLineCount() < text_view. getMaxLines()) { // do stuff } } });

How do I show ellipsis on android?

You can use android:ellipsize="start" or android:ellipsize="middle" according your need.

How do I limit characters in TextView?

you can extend the TextView class and overwrite the setText() function. In this function you check for text length or word cound. Better than counting the text length or the word cound a better way would be to use the "maxLines" attribute along with "ellipsize" attribute to attain the desired effect.


This is a common problem. Try using the following:

android:scrollHorizontally="true"
android:ellipsize="end" 
android:maxLines="1"

.............. the scrollHorizontally is the "special sauce" that makes it work.


This will also make a single line with ellipsise

 android:singleLine="true"

Use this

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

Don't use this without fully aware of what output comes

android:ellipsize="end"  
android:maxLines="1"

When you use maxlines = 1 it will some time truncate most of the characters.


The way it worked for me on multiple devices / APIs was programmatically like this (where tv is your TextView):

    if (tv.getLineCount() > 1) {
        int lineEndIndex = tv.getLayout().getLineEnd(0);
        String text = tv.getText().subSequence(0, lineEndIndex - 3) + "\u2026";
        tv.setText(text);
    }

So all the answers above cater to the requirement that only 1 line and then the ellipsis should appear. However if you want the ellipsis to appear after certain lines of text, then you should use the following:

android:ellipsize="end"
android:maxLines="2"
android:singleLine="false"

With this the ellipsis will appear only after 2 lines. Note: Its important to have singleLine as false.