How to tell if TextView text has been truncated due to singleLine=“true”?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to tell if TextView text has been truncated due to singleLine=“true”?



I have a TextView that I would like to know if it has been truncated due to singleLine="true" in the XML.



What are some ideas to achieve this without having to pass in the displayed text but to detect this with the TextView only?





This is the exact answer to your question: stackoverflow.com/questions/2923345/…
– Nigel Brown
36 mins ago







It is a possible solution but could we detect if the TextView is truncated with just the TextView itself and not having to pass in the resulting text?
– David
33 mins ago





all you really need is to see the length of the text once it is ellipsize and compare it to the line of text. here is another link with a good method: stackoverflow.com/questions/15567235/…
– Nigel Brown
18 mins ago





Thank you Nigel, that last answer you posted was exactly what I was looking for.
– David
11 mins ago





Great, i will update the answer for this question so people know its closed with what helped you
– Nigel Brown
7 mins ago




1 Answer
1



The answer the OP was looking for was found in the following link:
Check if textview is ellipsized in android



The answer basically uses this method to compare the length of the text to the ellipse count to tell if has been truncated.


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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

'Series' object is not callable Error / Statsmodels illegal variable name