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 .
NativePageSplitter.java
Activity Class
Download Source Code
Below the source code for splitting long html text by passing device height and width .
NativePageSplitter.java
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) {
mSpannableStringBuilder.append(charSequence);
}
public void split(TextPaint
textPaint) {
DynamicLayout staticLayout = new DynamicLayout(
mSpannableStringBuilder,
textPaint,
pageWidth,
null,
lineSpacingMultiplier,
lineSpacingExtra,
true
);
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;
}
else{
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;
}
}
package com.vj.pagesplitter;
import
android.app.Activity;
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>";
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics dimension = new DisplayMetrics();
getWindowManager().getDefaultDisplay()
.getMetrics(dimension);
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();
textPaint.setTextSize(15);
pageSplitter.append(Html.fromHtml(htmlStrng));
pageSplitter.split(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]);
}
}
}