Yamuna
Yamuna

Reputation: 139

android: webview shows blank screen some times while opening pdf in google doc

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();})()");
        }
    }

Upvotes: 8

Views: 2876

Answers (2)

Joseph Moore
Joseph Moore

Reputation: 384

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
            }
      }

Upvotes: 5

Sonny gao
Sonny gao

Reputation: 1

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.

Upvotes: 0

Related Questions