0

I am implementing an epub reader for android. I have successfully read the epub files and got the content to dynamic textviews and imageviews. So the content is actually made up with multiple textviews and imageviews.

Currently I have used a scrollview to read the book. Now I need to break the whole content in to pages(content of a page should based on the screen size).

For this I am thinking of using ViewPager. So it can be used to swipe between pages easily. But if you have solutions for other than that I will be a great help.

can anyone help to achieve this?

Thanks.

chathura
  • 3,362
  • 6
  • 41
  • 68
  • ViewPager seems to be fine option, are you having problems to get the content of one fragment in pager to be exact screen height? – Niko Aug 27 '14 at 09:20
  • yes, i don't know how to break the entire thing into pages based on the screen size. Once the content of the page is decided it can be loaded in to a fragment. – chathura Aug 27 '14 at 09:23
  • Are you placing the text and images into RelativeLayout, FrameLayout or LinearLayout? – Niko Aug 27 '14 at 09:31
  • LinearLayout inside a ScrollView – chathura Aug 27 '14 at 09:32

1 Answers1

1

Here is something to get you started with, the following component measures the text to fill the screen and the addTextmethod returns the text which was not fitted.

Combining with ImageView adds more complexity for measuring the height so I just made it to work with text. You could also adjust the values how many characters are removed to get iteration faster and also check for possibilities if this could be done more efficiently with line count combined with line height.

Notice that the component wants a long string which it will substring to whats left for next page.

public class ReaderLinearLayout extends LinearLayout {

    private static final int TEXT_SIZE = 44;
    private final DisplayMetrics dm = new DisplayMetrics();

    public ReaderLinearLayout(Context context) {
        super(context);
    }

    public ReaderLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public String addText(String text) {
        int height = getScreenHeight();

        String textLeftFromEnd = text;

        int textHeight = getTextHeight(text);

        while (textHeight > height) {
            text = text.substring(0, text.length() - 1);
            textHeight = getTextHeight(text);
        }

        TextView textView = new TextView(getContext());
        textView.setText(text);
        textView.setTextColor(Color.BLACK);
        textView.setTextSize(TEXT_SIZE);
        addView(textView);

        return textLeftFromEnd.subSequence(text.length(),
                textLeftFromEnd.length()).toString();
    }

    public int getTextHeight(final String text) {
        TextView textView = new TextView(getContext());
        textView.setText(text);
        textView.setTextSize(TEXT_SIZE);
        TextPaint textPaint = textView.getPaint();

        return new StaticLayout(text.toString(), textPaint, getScreenWidth(),
                Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true).getHeight();
    }

    public int getScreenHeight() {
        WindowManager wm = (WindowManager) getContext().getSystemService(
                Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(dm);

        return dm.heightPixels;
    }

    public int getScreenWidth() {
        WindowManager wm = (WindowManager) getContext().getSystemService(
                Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(dm);

        return dm.widthPixels;
    }
}
Niko
  • 8,093
  • 5
  • 49
  • 85
  • thanks Niko, I will try this and let you know the reult – chathura Aug 27 '14 at 10:05
  • how to get the number of pages? because for ViewPager's adapter we need to specify it in the getCount(). – chathura Aug 28 '14 at 06:20
  • 1
    You could make a helper class which could count the number of pages using the addText logic but not adding any views to anywhere. The page count would be number of how many times addText needs to be called with a String before it returns empty String back. – Niko Aug 28 '14 at 06:25
  • I'm bit confuse to implement this. can you suggest me a pseudocode so I can use that logic.(from getting the strings to attach them to viewpager) – chathura Aug 28 '14 at 06:40
  • can you help me to use this with imageviews? how to measure the height with imageviews? – chathura Aug 28 '14 at 11:48
  • 1
    http://stackoverflow.com/questions/4680499/how-to-get-the-width-and-height-of-an-android-widget-imageview – Niko Aug 29 '14 at 04:31