1

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 :((

2 Answers2

1

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
Christian
  • 7,062
  • 9
  • 53
  • 79
  • 1
    Not! int start = messageView.getLayout().getLineStart(1); Caused by: java.lang.NullPointerException at com.example.Poetry.PageAuthors.SetAuthorText(PageAuthors.java:116) This is not working. – Batista2015 Mar 10 '15 at 20:31
  • Did you understand the code? Or simply put it into your code and expected to work? Did getLayout() return anything different from null? – Christian Mar 10 '15 at 20:39
0

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;
            }
        });
Victor Marcoianu
  • 630
  • 2
  • 8
  • 20