Wednesday, November 25, 2015

Android native long text splitter like Magazine

This post about how to split long html string as pager like Magazine .

Below the source code for splitting long html text by passing device height and width .
package com.vj.pagesplitter;
import android.text.DynamicLayout;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;

public class NativePageSplitter {
    private final int pageWidth;
    private final int pageHeight;
    private final float lineSpacingMultiplier;
    private final float lineSpacingExtra;
    private final List<CharSequence> pages = new ArrayList<CharSequence>();
    private SpannableStringBuilder mSpannableStringBuilder = new SpannableStringBuilder();

    public NativePageSplitter(int pageWidth, int pageHeight, float lineSpacingMultiplier, float lineSpacingExtra) {
        this.pageWidth = pageWidth;
        this.pageHeight = pageHeight;
        this.lineSpacingMultiplier = lineSpacingMultiplier;
        this.lineSpacingExtra = lineSpacingExtra;

    public void append(CharSequence charSequence) {

    public void split(TextPaint textPaint) {
        DynamicLayout staticLayout = new DynamicLayout(
        int startLine = 0;
    // Log.e("SM", "pages :: ####### &&&&  " +staticLayout.getLineCount());
        while(startLine < staticLayout.getLineCount()) {
        // for(startLine =0; startLine< staticLayout.getLineCount(); startLine++) {
            int startLineTop = staticLayout.getLineTop(startLine);
            int endLine = staticLayout.getLineForVertical(startLineTop + pageHeight);
            int endLineBottom = staticLayout.getLineBottom(endLine);
            int lastFullyVisibleLine;
            if(endLineBottom > startLineTop + pageHeight){
                lastFullyVisibleLine = endLine - 1;
                lastFullyVisibleLine = endLine;
            int startOffset = staticLayout.getLineStart(startLine);
            Log.e("SM", "######## :lastFullyVisibleLine::::   " + lastFullyVisibleLine);
            int endOffset = staticLayout.getLineEnd(lastFullyVisibleLine);
            pages.add(mSpannableStringBuilder.subSequence(startOffset, endOffset));
            startLine = lastFullyVisibleLine + 1;

    public List<CharSequence> getPages() {
    //Log.e("SM", "pages :: ####### " +pages.size());
        return pages;


Activity Class

package com.vj.pagesplitter;

import android.os.Bundle;
import android.text.Html;
import android.text.TextPaint;
import android.util.DisplayMetrics;
import android.util.Log;

public class MainActivity extends Activity {

String htmlStrng ="<!DOCTYPE html><html><body><p>At the core of Android Studio is an intelligent code editor capable of advanced code completion, refactoring, and code analysis.The powerful code editor helps you be a more productive Android app developer..</p><p>This is a paragraph.</p><p>TAt the core of Android Studio is an intelligent code editor capable of advanced code completion, refactoring, and code analysis.The powerful code editor helps you be a more productive Android app developer.</p></body></html>";
    protected void onCreate(Bundle savedInstanceState) {
        DisplayMetrics dimension = new DisplayMetrics();
int w = dimension.widthPixels;
int h = dimension.heightPixels;

// PageSplitter_Articles pageSplitter = new PageSplitter_Articles(w-80, h-80
// , 1.3f, 1);

NativePageSplitter pageSplitter= new NativePageSplitter(w-70, h-70
, 1.2f, 1);
TextPaint textPaint = new TextPaint();

String[] arr = new String[pageSplitter.getPages().size()];
for (int i = 0; i < pageSplitter.getPages().size(); i++) {
arr[i] = pageSplitter.getPages().get(i).toString();
Log.e("SM", "arr[i] " +arr[i]);


Download Source Code

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...