0

I want to show PDF in a webView . I have a the URL of that PDF . It works at sometimes and shows the PDF but in sometimes doesn't show the PDF when I click on different URL in my RecyclerView . I searched for this Problem and I tries to use postDelayed but it still doesn't show the PDF at sometimes

Here is the Code

 final Dialog g = new Dialog(context);
                    //, android.R.style.Theme_Translucent_NoTitleBar
                    g.requestWindowFeature(Window.FEATURE_NO_TITLE);

                    g.setContentView(R.layout.webview_layout);

                    Window window = g.getWindow();
                    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
                    lp.copyFrom(g.getWindow().getAttributes());
                    Rect displayRectangle = new Rect();

                    window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);

                    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                    lp.height = WindowManager.LayoutParams.MATCH_PARENT;
                    lp.gravity = Gravity.CENTER_VERTICAL;

                    window.setAttributes(lp);

                    WebView webView = (WebView) g.findViewById(R.id.webView);
                   // WebSettings ws = webView.getSettings();
                    webView.getSettings().setJavaScriptEnabled(true);
                   // webView.getSettings().setPluginState(WebSettings.PluginState.ON);
                webView.getSettings().setBuiltInZoomControls(true);
                webView.getSettings().setSupportZoom(true);
                    webView.getSettings().setLoadWithOverviewMode(true);
                    webView.getSettings().setUseWideViewPort(true);
                webView.setWebViewClient(new Callback());
                webView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        webView.loadUrl( "http://docs.google.com/gview?embedded=true&url=" +list.get(position).getURl());

                    }
                },1000);
                g.show();

And the WebView Client

 private class Callback extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(
                WebView view, String url) {
            return(false);
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
      //      super.onReceivedSslError(view, handler, error);
            handler.proceed();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Log.e("webView"," "+"end");
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            Log.e("webView"," "+"start");
            return super.shouldOverrideUrlLoading(view, request);

        }
    }
Ahmed Elsayed
  • 231
  • 3
  • 23
  • `I want to show PDF in a webView ` That is impossible to begin with as a WebView cannot display pdf documents. A WebView is for html documents. – blackapps May 04 '21 at 18:16
  • U can do it by adding this Url before the PDF's Url http://docs.google.com/gview?embedded=true&url=+"PDF URL" – Ahmed Elsayed May 04 '21 at 19:37
  • Well what you call showing pdf. Do you know what that google service does actually? – blackapps May 04 '21 at 19:40
  • It sometimes success showing the PDF without using anything ,just the snipest code that I added above . But my problem is sometimes it doesn't show the pdf or anything and webView starts and ends but without showing anything . I don't know is the problem in the Webview code or what – Ahmed Elsayed May 04 '21 at 19:43
  • You only talk about what your WebView shows or not. And that we knew already. But i asked you: `Do you know what that google service does actually with the pdf file?` What is the reason you would call such a service? – blackapps May 04 '21 at 19:45
  • I think it just show to you the PDF and you can zoom in and zoom out – Ahmed Elsayed May 04 '21 at 19:47
  • Well I will tell you . My real code is using Intent to launch the PDF Program in the mobile and showing the PDF . But if the mobile doesn't have a PDF Program , then I used the WebView approach – Ahmed Elsayed May 04 '21 at 19:48
  • No. That service does not show anything. Its the WebView that shows. But what is that service doing with that pdf document? `I used the WebView approach ` .. Well you use a google service approach i would say. But why would that help? I'm pretty amazed that you calll a service and send/indicate a pdf document and that you do not know what you get back. – blackapps May 04 '21 at 19:48
  • `But what is that service doing with that pdf document` mmm I don't know actually but I'm sure of that way is the only way to show PDF using WebView by while using a URL . All answers on Stack use that way – Ahmed Elsayed May 04 '21 at 19:52
  • Now think and you will know what that service does. 1. A WebView cannot display a pdf document. 2. A WebView can display a html document. Please combine 1 and 2 and tell what you get back from the service. And what the service has done. – blackapps May 04 '21 at 19:54
  • Ok but why it actually worked ? – Ahmed Elsayed May 04 '21 at 19:58

0 Answers0