Using getEllipsisCount will not work with text that has blank lines. I used the following code to make it work:
message.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { if(m.isEllipsized == -1) { Layout l = message.getLayout(); if (message.getLineCount() > 5) { m.isEllipsized = 1; message.setMaxLines(5); return false; } else { m.isEllipsized = 0; } } return true; } });
Remember to set maxLineCount in your XML. Then you can check the lineCount in your code, and if it is more than a certain number, you can return false to cancel the TextView drawing, and set the number of lines, as well as a flag to keep the text looking too long or not. The text view will again be drawn with the correct number of lines, and you will find out if its ellipsis or not with the flag.
Then you can use the isEllipsized flag to perform all necessary actions.
js123 Feb 14 '15 at 17:18 2015-02-14 17:18
source share