Redman Redman - 23 days ago 7
Javascript Question

WebView get maximum scroll width

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.

Answer

i under stood the problem and the solution was to keep a countdown timer and giving 1 sec delay before calculating the width

here is how i did it

 @Override
public void onPageFinished(final WebView view, String url) {


    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;')";

    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;"+
    "function getPagesCount() {"+
      "  android.getPagesCount(pageCount);"+
  "  };";





    myWebView.loadUrl("javascript:" + varMySheet);
    myWebView.loadUrl("javascript:" + addCSSRule);
    myWebView.loadUrl("javascript:" + insertRule1);





    view.setVisibility(View.VISIBLE);
    super.onPageFinished(view, url);
  CountDownTimer test=  new CountDownTimer(3000,1000) {

        @Override
        public void onTick(long millisUntilFinished) {


        }

        @Override
        public void onFinish() {

            int widthis =  webView.getContentWidth();

            TotalScrollWidth=widthis;
                    Log.i("WidthTotoal Scroll", ""+widthis);
                    TotalPages= (TotalScrollWidth/ScreenWidth)-1;
                    Toast.makeText(getApplicationContext(), "Total Pages are" +TotalPages, 100).show();
                     progressPages.setMax(TotalPages);
                     progressdialog.dismiss();
        }
    };


    test.start();



}