user1763198
user1763198

Reputation: 83

TextView - get Visible text, and how to divide string to pages?

What i want to do is to divide long text to pages, so I can display them one by one in one text view, when user hits next/prev button. What I need is to know te last displayed character index, but thats not so simple....

I have tried this:

int start = textView.getLayout().getLineStart(0);
int end = textView.getLayout().getLineEnd(textView.getLineCount() - 1);

String displayed = textView.getText().toString().substring(start, end);

frrom LINK

But getLayout returns null

Also using getWidth/getHeight is useless cause they returns 0 in "onCreate" function...

Thanks for any help !

Upvotes: 2

Views: 3452

Answers (2)

user1938357
user1938357

Reputation: 1466

I figured out a much efficient way to display a long string across number of pages. -I display the long text into the text view and disable scrolling -I then find out the lines in the layout and lines on single screen, hence I know how many screens(pages) will the long string span across -Then whenever user clicks a button to go to next page or swipes screen to go to the next page then I use TexView.scrollTo() function to move to number of lines(as many on a screen) forward. So it becomes as good as moving to next page

Upvotes: 2

Todd Sjolander
Todd Sjolander

Reputation: 1569

It might make more sense to approach the problem from the other direction: You have a long String of text. Then you decide how much you can fit on a page, and break the String into an array of multiple substrings, one for each page. Then you can simply store a page index someplace, and use that as an index into the array of String that you made when you broke the entire text into substrings.

This assumes that there's an algorithm to find how much text you can fit in a page. Take a look at this question.

Upvotes: 3

Related Questions