2

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 !

Community
  • 1
  • 1
user1763198
  • 83
  • 1
  • 10

2 Answers2

3

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.

Community
  • 1
  • 1
Todd Sjolander
  • 1,459
  • 1
  • 15
  • 28
  • OK - that was usefull. Now I want to use: getPaint().breakText(mock_string,true, width , null); But.... how can I get textView Width in "onCreate" ? tried: contentText.measure(0, 0); int width = contentText.getMeasuredWidth(); But now width has no sense (about 14k) – user1763198 Nov 05 '12 at 16:24
  • You can't get width from `View.getWidth()` in `onCreate` because that information hasn't been passed down to the Views yet. However, try this to get general screen metrics: `DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);` That will give you height and width of the entire screen in pixels. Depending on your layout, you might have enough information there to determine (or make a good enough guess about) your TextView's width. – Todd Sjolander Nov 05 '12 at 16:47
  • It looks like you really helped me. I Measured my display, and loaded from dimension my paddings and margins. Then I calculated actual textview width, and method breakText() seems to be working well. When i finnish my method i will post it as answer since, lot of people asked simmilar questions. Thanks ! – user1763198 Nov 05 '12 at 17:17
2

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

user1938357
  • 1,466
  • 3
  • 20
  • 33