FrederickCook FrederickCook - 10 months ago 57
Android Question

How do I tell if my textview has been ellipsized?

I have a multi-line

that has
set. I would like to know, however, if the string I place in there is actually too long (so that I may make sure the full string is shown elsewhere on the page).

I could use
and find about what the approximate length of string will fit, but since it's multiple lines, the
handles when to wrap, so this won't always work.

Any ideas?


You can get the layout of the TextView and check the ellipsis count per line. For an end ellipsis, it is sufficient to check the last line, like this:

Layout l = textview.getLayout();
if (l != null) {
    int lines = l.getLineCount();
    if (lines > 0)
        if (l.getEllipsisCount(lines-1) > 0)
            Log.d(TAG, "Text is ellipsized");

This only works after the layout phase, otherwise the returned layout will be null, so call this at an appropriate place in your code.