I have a multi-line TextView
that has android:ellipsize="end"
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 TextView.length()
and find about what the approximate length of string will fit, but since it's multiple lines, the TextView
handles when to wrap, so this won't always work.
Any ideas?
public int getEllipsisCount (int line): Returns the number of characters to be ellipsized away, or 0 if no ellipsis is to take place. So, simply call : int lineCount = textview1.
Android Ellipsize Android TextView ellipsize property Causes words in the text that are longer than the view's width to be ellipsized ( means to shorten text using an ellipsis, i.e. three dots …) instead of broken in the middle to fit it inside the given view.
If you have a new text to set to the TextView , just call textView. setText(newText) , where newText is the updated text. Call this method whenever newText has changed.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With