Search code examples
javaandroidandroid-studiowebviewscrollto

WebView.scrollTo function not showing up


I'm trying to open up a pdf document using a webview and scroll to a certain page. However, the logs show that the app thinks it scrolled to a page, but is still stuck on page 1 of the document.

My current code is as follows:

String pdf = https://docs.google.com/file/d/0BzfmteGs4MHZMTAzOGQ0ZjUtM2E5OC00YmNhLWIyMDEtZDAxYjc5OWIwYjkw/edit?hl=en#
web = (WebView) findViewById(R.id.web);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl(pdf);
web.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        web.scrollTo(0,100);
        Log.d("CURRENTPAGE", String.valueOf(web.getScrollY()));
        Log.d("TOTALPAGES", String.valueOf(view.getContentHeight()));
    }
});

The logs say this

CURRENTPAGE: 100
TOTALPAGES: 796

Yet the page remains on page 1 of the pdf. Can anyone help me find what I'm doing wrong?


Solution

  • WebView doesn't handle PDF itself. if you are opening some URL inside (no download-as-file-prompt) then this is probably some web application, which is handling internally PDF format, "translating" its content to WebView-understandable (some web lang, HTML probably)

    so if this web app is introducing its content as single web page with 796px height (you named it TOTALPAGES, thats not true) and handling pdf-page switching internally then you can't do much in that manner

    further: your loaded webpage have 796px height and probably fit whole content into your screen, so scrollTo method won't have anything to scroll. if your web page height got e.g. 3456 px and your screen is full HD (so web page won't fit) then you may use scrollTo