Hi guys, I am creating an epub reader and have downloaded the book in an Android web browser. and also made a webview to move horizontally, referring to this, but now I want the web view to move like pages, so I want to do this 1. Calculate the total horizontal width and width of the screen, now divide them to get the total number of pages 2 .block scrolling of the web profile and, when the user runs, set the scrolling of webview to the screen width * swipeCount
Now the problem is that when I try to get totalHorizontalScrollWidth, it gives the width of the screen, I tried these methods, but both give the 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(); } });
returns 480 screen widths
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 one
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 a result like 480, which is the screen size
I also came across this script, but don't know how to get the values ββfrom this script in 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;";
someone please help me get the maximum webview scrollbar width or some alternative to get an effect like page rotation.