Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting content of a particular line in a multiline edittext in android

Tags:

android

I have a multiline edit text in which the number of lines can grow up to any number.

Does android provide any functionality to get content of any particular line?

Suppose total number of lines are three and i want to read only second line.

like image 386
vishy Avatar asked Mar 23 '23 21:03

vishy


1 Answers

A EditView in Android does wrap its text so it fits in the view. That means that you have to determine a lines start and end position to read it and then you can extract it from the full contentstring. This post already explained this issue.

So your code would be:

 // change to your needs
 int linenumber = 1;

 int startPos = myTextView.getLayout().getLineStart(linenumber);
 int endPos = myTextView.getLayout().getLineEnd(linenumber);

 String theLine = myTextView.getText().toString().substring(startPos, endPos);
like image 126
alex Avatar answered Mar 25 '23 10:03

alex