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?