Siddhesh Pawar
Siddhesh Pawar

Reputation: 259

Android Web view not displaying PDF

I have an URL which displays PDF on the browser, I have a created a WebView but it's not showing the pdf.

I have provided all the setting to the WebView, a WebViewClient as well but the ProgressBar just starts and the pdf is not being loaded.

class KnowledgeShareWebview : AppCompatActivity() {

    lateinit var webView: WebView
    lateinit var progressBar: ProgressBar
    var url = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_knowledge_share_webview)
        webView = findViewById(R.id.webView)
        progressBar = findViewById(R.id.progressBar)

        if (intent != null) {
            url = intent.getStringExtra("url")
        }

        Helper.printMessege(KnowledgeShareWebview::class.java.simpleName, "url....$url")
        initWebView()
        webView.loadUrl(url)

    }

    private fun initWebView() {
        webView.settings.loadWithOverviewMode = true
        webView.settings.useWideViewPort = true
        webView.clearCache(true)
        webView.clearHistory()
        webView.settings.javaScriptEnabled = true
        webView.settings.useWideViewPort = true
        webView.isHorizontalScrollBarEnabled = false
        webView.webViewClient = object : WebViewClient() {
            override fun onPageStarted(view: WebView, url: String, favicon: Bitmap) {
                super.onPageStarted(view, url, favicon)
                progressBar.visibility = View.VISIBLE
            }

            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                webView.loadUrl(url)
                return super.shouldOverrideUrlLoading(view, url)
            }

            override fun onPageFinished(view: WebView, url: String) {
                super.onPageFinished(view, url)
                progressBar.visibility = View.GONE
                invalidateOptionsMenu()
            }

            override fun onReceivedError(view: WebView, request: WebResourceRequest, error: WebResourceError) {
                super.onReceivedError(view, request, error)
                progressBar.visibility = View.GONE
            }
        }
    }
}

Upvotes: 1

Views: 7615

Answers (2)

Naveen
Naveen

Reputation: 252

Android is the ability to display a PDF document within a WebView. However, the WebView does not contain a PDF plugin that allow you to display a PDF document. One solution is to use an Intent object to launch a third-party app (such as Adobe Acrobat) which can handle the PDF document. However, this will transfer control over to the thrid-party app.

If you insists on displaying the PDF document using a WebView, you can use the following trick. You can use Google Docs to open your PDF document and then load the URL of Google Docs using the WebView.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    WebView webView=new WebView(MainActivity.this);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setPluginState(PluginState.ON);

    //---you need this to prevent the webview from
    // launching another browser when a url
    // redirection occurs---
    webView.setWebViewClient(new Callback());

    String pdfURL = "url";
   webView.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdfURL);

    setContentView(webView);
}

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

Upvotes: 2

Gopal Awasthi
Gopal Awasthi

Reputation: 433

You have to call webview.load(url).

webView.Settings.JavaScriptEnabled = true;
        webView.Settings.SupportZoom();
        webView.Settings.SetAppCacheEnabled(true);
        webView.Settings.DomStorageEnabled = true;
        webView.ZoomOut();
        webView.ZoomIn();
        webView.Settings.BuiltInZoomControls = true;
        webView.Settings.LoadWithOverviewMode = true;
        webView.Settings.UseWideViewPort = true;
        //webview.Settings.SetSupportZoom (true);
        webView.Settings.SetPluginState(WebSettings.PluginState.On);
        webView.Settings.GetPluginState();
        webView.LoadUrl(externalurl);
        //after loading url call the webviewclient
        webView.SetWebViewClient(new MonkeyWebViewClient(imgViewBack, imgViewForward, imgRefresh));
        webView.SetWebChromeClient(new WebChromeClient());

Upvotes: 0

Related Questions