5

I have a long text and fixed size textView. How can show the text page by page? Users will interactive with the program in this way: He swipe left or right to turn to next or previous page.

Currently, I create a PageManager to do this job. But it's very limited. The core code to process the text is:

while (true) {
        newLineIndex = TextUtils.indexOf(content, '\n', processIndex);
        if (newLineIndex == -1) {// till the end of content
            charCount = paint.breakText(content, processIndex, content.length(), false, leftLines * lineWidth, width);
        } else {
            charCount = paint.breakText(content, processIndex, ++newLineIndex, false, leftLines * lineWidth, width);
        }

        leftLines = (int) ((leftLines * lineWidth - width[0]) / lineWidth);
        processIndex += charCount;

        if (leftLines < 1 || processIndex >= content.length()) {
            page = new Page();
            page.endIndex = processIndex;
            page.startIndex = pageBaseLine;
            page.content = content.subSequence(page.startIndex, page.endIndex);
            result.add(page);
            pageBaseLine = processIndex;

            leftLines = lineNumber;
        }

        if (processIndex >= content.length()) {
            break;
        }
    }

The limitation is the page may truncate the text like

|A lon|
|g wor|
|d man|

// a long word man

or incorrec lines due to word wrapping:

//Page Manager calculates this(2 lines):

|a sentence with loooooooo|

|ooong word abcdefghijklmn|

//But actually in text view(3 lines):

|a sentence with           |

|looooooooooong word      |

|abcdefghijklmn           |

So the final line count is more than calculation. So my page manager is stupid. Would any one help me? thanks!

Ghasem
  • 14,455
  • 21
  • 138
  • 171
Henry Sou
  • 882
  • 2
  • 12
  • 19
  • Since it is an algorithm related question, maybe you should consider posting it in http://programmers.stackexchange.com/ – Arnab Chakraborty Dec 03 '11 at 15:16
  • See my answer here http://stackoverflow.com/questions/20204348/how-to-break-styled-text-into-pages-in-android – mixel Nov 25 '13 at 22:07

1 Answers1

5

It might be too late for answer, but I'll post how I solved problem like this.

I made similar project, but for android tablet. In my application, I have system bottom bar, my navigation bar and header. These parts of Activity don't take part in text preview, so I want to exclude them in calculations. For screen 750px these parts takes 24% of screen, it's about 180px. So I use the following algorithm to view text in text view:

  1. Calculate actual height to view text, it's height of my textview: ScreenHeight - ScreenHeight*0.24
  2. Set text size, for example 14.0
  3. Calculate number of lines in text file
  4. Calculate number of lines per page: Actual height/ text size
  5. Calculate number of pages and load them in HashMap

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight() — 0.24*display.getHeight();
mLinesInPage = (int) (Math.floor(height / mTextSize));
mPages = (int) (Math.ceil(mLinesInText / mLinesInPage));

Formatting and other transformations executes "on the fly". Nothing difficult, but works for me. There are examples

Before using algorithm:

enter image description here

After using algorithm:

enter image description here

enter image description here

sigrlami
  • 1,822
  • 1
  • 17
  • 35
  • 1
    I have same issue, but i cant understand how your code is solving a problem... Maybe its because i dont get how you calculate 'mLinesInText'. Can you explain a bit more please @Sigrlami – kort.es Jul 27 '13 at 23:34
  • @kort.es You can get *mLinesInText* by reading hole text and counting newline delimiter *"\n"*. Ofcourse, if you reformat text in some way, this parameter should be recalculated too. – sigrlami Jul 29 '13 at 19:05
  • is every device have to get actual height screen with this way? ScreenHeight - ScreenHeight*0.24 (always substract with ScreenHeight*0.24) ?? – dreamfighter May 14 '14 at 04:03
  • 1
    @dreamfighter 0.24 is empirical value, because your actual screen for app will be smaller due to action bar on top and system bar on bottom. It may change for different devices. – sigrlami May 14 '14 at 08:39
  • @BaDo this is an actual application. I can't share my code. Ask SO another question with specific problem and I'll try to help you – sigrlami Jun 06 '15 at 09:00