13

Is there a way to check whether a TextView's text is truncated in my code?

0

3 Answers 3

13

You can use method: getEllipsisCount

 public static boolean isTextTruncated( String text, TextView textView )
    {
        if ( textView != null && text != null )
        {

            Layout layout = textView.getLayout();
            if ( layout != null )
            {
                int lines = layout.getLineCount();
                if ( lines > 0 )
                {
                    int ellipsisCount = layout.getEllipsisCount( lines - 1 );
                    if ( ellipsisCount > 0 )
                    {
                        return true;
                    }
                }
            }

        }
        return false;
    }

I found the answer at the following related post:

Check if textview is ellipsized in android

2
  • 2
    What is the use of param String text ? Nov 9, 2017 at 5:15
  • getLayout return null with me Apr 3, 2022 at 9:30
0

Don't think so but you can read here to properly handle it if you only want the text to use one line.

0

Check out this topic

... You can create a Paint object with TextView2's text size and use breakText() to measure how many characters will fit in your TextView2's width...

It works for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.