Reputation: 37
Is it possible to know the number of visible words? not full length?
I add to textView string from 100 words. But visible only 10 words. How do I know that only 10 words?
Log.e("asdas", String.valueOf(messageView.length()));
not working
Sorry if this question is too complex :((
Upvotes: 1
Views: 428
Reputation: 630
For anybody interested and getting the NullPointerException, here's a working example of Christian's answer:
ViewTreeObserver vto = textView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int start = textView.getLayout().getLineStart(0);
int end = textView.getLayout().getLineEnd(textView.getLineCount() - 1);
String displayed = textView.getText().toString().substring(start, end);
int visibleWords = displayed.split(" ").length;
}
});
Upvotes: 0
Reputation: 7320
I don't know WHY you need this, but here it goes:
int start = textView.getLayout().getLineStart(0);
int end = textView.getLayout().getLineEnd(textView.getLineCount() - 1);
String displayed = textView.getText().toString().substring(start, end);
int visibleWords = displayed.split(" ").length
Upvotes: 1