Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TextView and getting line of text

I have a TextView whose text can run to many lines. Once it has been created and set dynamically, I'd like to

  1. Get the text on a given line, and
  2. Know the width and height of this line.

How do I do this?

like image 685
SK9 Avatar asked Feb 17 '12 09:02

SK9


1 Answers

If I understand correctly the answer to question 2 is:

textView.getLineBounds (int line, Rect bounds)

The width in pixels should be abs(bounds.right - bounds.left); and the height is abs(bounds.bottom - bounds.top)

Your first question is a bit more tricky, but something like this should do the required magic:

Layout layout = textView.getLayout();
String text = textView.getText().toString();
int start=0;
int end;
for (int i=0; i<textView.getLineCount(); i++) {
    end = layout.getLineEnd(i);
    line[i] = text.substring(start,end);
    start = end;
}
like image 126
CjS Avatar answered Sep 22 '22 00:09

CjS