Prosanto
Prosanto

Reputation: 924

How i can show a long textview(string ) into multiple page of android

I am developing an application which looks like an eBook reader(.text ,pdf file etc). I have a huge text which is divided into various chapters or page .

Now the problem is how to divide the whole content into number of pages and show those page one by one. How can I know that, the number of characters that are going to fit into the screen( depending on the screen size and font size). I am totally confused on where to start and how to proceed.Please help to me by give to example.

Upvotes: 4

Views: 4286

Answers (2)

korosmatick
korosmatick

Reputation: 579

Here is a simple sample application incase anybody else needs it https://github.com/koros/AndroidReader

import android.content.Context;
import android.os.AsyncTask;
import android.text.TextPaint;

public class PagerTask extends AsyncTask<MainActivity.ViewAndPaint,      MainActivity.ProgressTracker, Void> {

private Context mContext;

public PagerTask(Context context){
    this.mContext = context;
}

protected Void doInBackground(MainActivity.ViewAndPaint... vps) {

    MainActivity.ViewAndPaint vp = vps[0];
    MainActivity.ProgressTracker progress = new MainActivity.ProgressTracker();
    TextPaint paint = vp.paint;
    int numChars = 0;
    int lineCount = 0;
    int maxLineCount = vp.maxLineCount;
    int totalCharactersProcessedSoFar = 0;

    // contentString is the whole string of the book
    int totalPages = 0;
    while (vp.contentString != null && vp.contentString.length() != 0 )
    {
        while ((lineCount < maxLineCount) && (numChars < vp.contentString.length())) {
            numChars = numChars + paint.breakText(vp.contentString.substring(numChars), true, vp.screenWidth, null);
            lineCount ++;
        }

        // Retrieve the String to be displayed in the current textview
        String stringToBeDisplayed = vp.contentString.substring(0, numChars);
        int nextIndex = numChars;
        char nextChar = nextIndex < vp.contentString.length() ? vp.contentString.charAt(nextIndex) : ' ';
        if (!Character.isWhitespace(nextChar)) {
            stringToBeDisplayed = stringToBeDisplayed.substring(0, stringToBeDisplayed.lastIndexOf(" "));
        }
        numChars = stringToBeDisplayed.length();
        vp.contentString = vp.contentString.substring(numChars);

        // publish progress
        progress.totalPages = totalPages;
        progress.addPage(totalPages, totalCharactersProcessedSoFar, totalCharactersProcessedSoFar + numChars);
        publishProgress(progress);

        totalCharactersProcessedSoFar += numChars;

        // reset per page items
        numChars = 0;
        lineCount = 0;

        // increment  page counter
        totalPages ++;
    }

    return null;
}

    @Override
    protected void onProgressUpdate(MainActivity.ProgressTracker... values) {
        ((MainActivity)mContext).onPageProcessedUpdate(values[0]);
    }

}

enter image description here

Upvotes: 2

asloob
asloob

Reputation: 1326

Here is how PageTurner does it. It takes a single scroll view to display the long text string. But, the scroll gestures are overridden. So when you do a "swipe left gesture" it will do a Page Flip animation and scroll the view downwards (by screen height). So the user feels as if the page has turned when essentially you have just manually scrolled to the bottom of the view. Ingenius isn't it? And you don't have to worry about the font size, paragraph spacing, words being cut, etc. The app is available on google play.

Upvotes: 1

Related Questions