3

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?

David
  • 171
  • 2
  • 10
  • This is the exact answer to your question: https://stackoverflow.com/questions/2923345/is-there-a-way-to-check-if-a-textviews-text-is-truncated – Nigel Brown Jul 27 '18 at 17:05
  • 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 Jul 27 '18 at 17:07
  • 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: https://stackoverflow.com/questions/15567235/check-if-textview-is-ellipsized-in-android – Nigel Brown Jul 27 '18 at 17:22
  • Thank you Nigel, that last answer you posted was exactly what I was looking for. – David Jul 27 '18 at 17:29
  • Great, i will update the answer for this question so people know its closed with what helped you – Nigel Brown Jul 27 '18 at 17:34
  • Thank you Nigel. – David Jul 27 '18 at 17:55

1 Answers1

4

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");
        } 
    } 
}
Nigel Brown
  • 440
  • 2
  • 13