0

I want to calculate the number of visible characters in TextView. Suppose that you have 1 million characters and the TextView can only show 100 first ones. I want to get the number "100" as the result. Currently, I use this code:

myTextView.setText(largText);
myTextView.getViewTreeObserver().addOnGlobalLayoutListener
    (new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Layout layout = myTextView.getLayout();
            int numOfLines = layout.getLineForVertical(myTextView.getHeight());
            int n = layout.getLineEnd(numOfLines); // get the result
        }
});

But this method is very slow to paginate large texts. Is there any faster method that can calculate the result?

Payam Roozbahani
  • 1,225
  • 15
  • 27

1 Answers1

0

I would count characters in textfield and compare it to Max lenght of the textfield, if its bigger then max shown is your result else if its less then just return count characters.

Saeed Joul
  • 244
  • 2
  • 15