6

I am using webview in a fragment to view online PDF using http://docs.google.com/viewerng/viewer?embedded=true&url. The PDF file has many pictures and chart. It load the PDF perfectly. But some times it shows blank screen. I added the below code.

webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);

After adding this, I am unable to recreate the issue. But the above two lines are not supported in playstore. I have tried all the Links to this issue. I mostly see the blank screen in the devices with API>23.

@kkarakk Sorry for late response..Please find my code below

 webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            webView.getSettings().setBuiltInZoomControls(true);
            webView.getSettings().setSupportZoom(true);
     /*     webView.getSettings().setAllowFileAccessFromFileURLs(true);
            webView.getSettings().setAllowUniversalAccessFromFileURLs(true);*/
            webView.setWebViewClient(new MyWebViewClient());
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl("http://docs.google.com/gview?embedded=true&url=MYURL");

 private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            webView.loadUrl("javascript:(function() { document.querySelector('[role=\"toolbar\"]').remove();})()");
        }
    }
Yamuna
  • 119
  • 1
  • 8
  • please put the xml layout file of the webview and also how you're loading content into it ie code – Karan Harsh Wardhan Jan 19 '19 at 06:50
  • You can [edit](https://stackoverflow.com/review/first-posts/21972581#) to format the question better. Make use of `markdown` syntax and the buttons on the editor. You can check other well received questions to get the idea. – Til Jan 19 '19 at 06:59
  • Would you be able to post the URL of an example pdf that has the issue? – Jake Lee Feb 06 '19 at 11:04
  • @JakeSteam Sample URL: [link](http://www.pdf995.com/samples/pdf.pdf). I got this error in logcat: chromium: [INFO:CONSOLE(1)] "Uncaught TypeError: Cannot read property 'remove' of null", source: (1). **onLoadResource** is failed to execute sometimes – Yamuna Feb 06 '19 at 12:19
  • 3
    Any answer to this? I'm having the same exact problem. Sometimes it loads up fine, other times I get just a blank white page. – Phil Apr 23 '19 at 14:47
  • 1
    @Phil.I got this error mostly in Samsung devices But no solution yet.. – Yamuna Apr 30 '19 at 08:25

2 Answers2

4

Similar to Sonny's Answer but I found comparing if the title was there was more reliable. I also put an attempt count in incase it was just the url erroring so it wasn't attempting forever.

      public void onPageFinished(WebView view, String url) {
            if (view.getTitle().equals(""))
                view.loadUrl(url)
            } 
            else {
               //Success
            }
      }
Joseph Moore
  • 364
  • 2
  • 8
  • I was having same issue. I debug my code it wasn't throwing error but directly calling page finished without any error so I also recursively call like you and it works for me too – vivek panchal Aug 10 '20 at 05:41
0

add "webView.getContentHeight()" validation in onPageFinished(WebView view, String url)

 if (webView.getContentHeight() == 0)
{

    webView.loadUrl("https://docs.google.com......");// RE-Loading

}

I have tested and works fine.

Sonny gao
  • 1
  • 3