Hello guys i am creating epub reader and loaded the book in android webview and also made webview to move horizontally refering this but now i want to make webview to move like pages so i want to do this way 1.calculate the total horizontalscroll width and screen width , now divide them to get total pages 2.lock the scroll of webview and when ever user swipes ,set the scrolling of webview to screenwidth*swipeCount
now the problem is when i am trying to get totalHorizontalScrollWidth it is giving screen width i tried these ways but both giving same result
FirstMethod
ViewTreeObserver vto = webView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
webView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
Log.e("ScrollWidth",""+webView.getMeasuredWidth());
Toast.makeText(getApplicationContext(), "new Width is"+webView.getMeasuredWidth(), Toast.LENGTH_SHORT).show();
}
});
returning 480 screen width
and SecondMethod
public class WebViewWidth extends WebView {
public WebViewWidth(Context context) {
super(context);
}
public WebViewWidth(Context context, AttributeSet attrs) {
super(context, attrs);
}
public int getContentWidth()
{
return this.computeHorizontalScrollRange();
}
public int getContentHeight()
{
return this.computeVerticalScrollRange();
}
}
and used this webView to get computeHorizontalScrollRange like this
private class MyWebViewClient extends WebViewClient {
private Context mContext;
public MyWebViewClient(Context context) {
this.mContext = context;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
mProgressDialog.dismiss();
final WebView myWebView = (WebView) view;
String varMySheet = "var mySheet = document.styleSheets[0];";
String addCSSRule = "function addCSSRule(selector, newRule) {"
+ "ruleIndex = mySheet.cssRules.length;"
+ "mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);"
+ "}";
String insertRule1 = "addCSSRule('html', 'padding: 0px; height: "
+ (myWebView.getMeasuredHeight() / mContext.getResources().getDisplayMetrics().density)
+ "px; -webkit-column-gap: 0px; -webkit-column-width: "
+ myWebView.getMeasuredWidth() + "px;')";
myWebView.loadUrl("javascript:" + varMySheet);
myWebView.loadUrl("javascript:" + addCSSRule);
myWebView.loadUrl("javascript:" + insertRule1);
super.onPageFinished(view, url);
int widthis = webView.getContentWidth();
Toast.makeText(getApplicationContext(), "Width is"+widthis, Toast.LENGTH_SHORT).show();
Log.i("WidthTotoal Scroll", ""+widthis);
}
}
Even this gives the result as 480 which is the screen size
i also came across this script but dont know how to get values from this script to android
String js = ""+
"var desiredHeight;"+
" var desiredWidth;"+
" var bodyID = document.getElementsByTagName('body')[0];"+
" totalHeight = bodyID.offsetHeight;"+
"pageCount = Math.floor(totalHeight/desiredHeight) + 1;"+
" bodyID.style.padding = 10;"+ //(optional) prevents clipped letters around the edges
" bodyID.style.width = desiredWidth * pageCount;"+
" bodyID.style.height = desiredHeight;"+
" bodyID.style.WebkitColumnCount = pageCount;";
some one please help me to get the maximum horizontal scroll width of webview or some alternative to get effect like turning pages.