0

I added WebView to Android. It's working fine. But my website contains PDF links. When click on PDF link on web view, it will open via default web browser, not in WebView. I want to display PDF also in WebView. Is there any way to restrict it to open in the same WebView?

Shashanth
  • 4,995
  • 7
  • 41
  • 51
Mr.Soft
  • 143
  • 2
  • 14
  • 1
    Possible duplicate of [Display the pdf file(stored in google drive) in webview by using google docs](https://stackoverflow.com/questions/39220941/display-the-pdf-filestored-in-google-drive-in-webview-by-using-google-docs) – Jaswant Singh Sep 03 '18 at 04:34
  • You will get answer on this [link](https://stackoverflow.com/a/5296125/5995648) – Arbaz.in Sep 03 '18 at 04:37
  • Possible duplicate of [Link should be open in same web view in Android](https://stackoverflow.com/questions/7308904/link-should-be-open-in-same-web-view-in-android) – Jaydip Kalkani Sep 03 '18 at 04:38
  • @JaswantSingh Actually I don't have pdf link.Only I have web link.Web link works in web view nicely.But it contains pdf links.Those are not opening in web view.That's the problem.... – Mr.Soft Sep 03 '18 at 04:43
  • 2
    Possible duplicate of [How can I display a pdf document into a Webview?](https://stackoverflow.com/questions/2655972/how-can-i-display-a-pdf-document-into-a-webview) – Manohar Sep 03 '18 at 04:55
  • See this [Link](http://weimenglee.blogspot.com/2013/05/android-tip-displaying-pdf-document.html) – Sniffer Sep 03 '18 at 05:27

2 Answers2

2

Try this:

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);
    webView.setWebViewClient(new Callback());
    String pdfURL = "http://dl.dropboxusercontent.com/u/37098169/Course%20Brochures/AND101.pdf";
    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);
    }
}

like this

enter image description here

Android Geek
  • 8,956
  • 2
  • 21
  • 35
  • I don't have pdf link.Only I have web site link...the problem is when click on the pdf link on website,it will open via default browser.... – Mr.Soft Sep 03 '18 at 06:53
  • try to donwload the pdf and then upload it to archieve.org for making it direct link ... then use the code above given by @AndroidGeek.... – gumuruh Apr 05 '20 at 09:32
0

I found a really simple solution (without google drive viewer etc.). It will open the pdf document in a preview window where the user is able to view and save/share the document:

webView.setDownloadListener(DownloadListener { url, userAgent, contentDisposition, mimetype, contentLength ->
     val i = Intent(Intent.ACTION_QUICK_VIEW)
     i.data = Uri.parse(url)
     startActivity(i)
})

(Kotlin)

Dion
  • 3,145
  • 20
  • 37