Skip to main content

How to open pdf file url using webview in Android

 Sometimes we have requirement of showing pdf file in our Android Application. Although there are many third party gradle dependencies available online by which you can show your pdf file easily but one of the major drawback of using these libraries are they will increase you apk size .In some cases they will increase apk size upto ~14 MB. Alternatively you can access and show pdf file using assets and from storage options. This can take less size than any third party library.

Here we use pdf using url and if you directly open your pdf file url in webview you will get no result because it will start downoad if you open this link in any web browser. 

So in this article we will learn how we can open our pdf url using webview and without using any third party library. So lets Start step by Step:-

Lets Suppose you have a pdf file url something like

 http://yourwebsite.com/files/mydocumentfile.pdf

Step 1:

So we you this pdf file url to embed in our webview .First of all you have to host your pdf url on Google Drive or on Google Documents. Here we use Google Docs Viewer for our pdf url.

Step 2:

Your code will look like this

public class PDFWebView extends AppCompatActivity {
    WebView webView;
String mainUrl="https://docs.google.com/gview?embedded=true&url=http://yourwebsite.com/files/mydocumentfile.pdf";
private String removePdfTopIcon = "javascript:(function() {document.querySelector('[class=\"ndfHFb-c4YZDc-Wrql6b\"]').remove();})()";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pdfwebview);

        webView=(WebView)findViewById(R.id.webview);

        webView.loadUrl(removePdfTopIcon);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSupportZoom(true);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) 
            {

            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) 
            {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                webView.loadUrl(removePdfTopIcon);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

                webView.loadUrl(removePdfTopIcon);
            }
        });

        webView.loadUrl(mainUrl);
       
    }

  
}

Here we use https://docs.google.com/gview?embedded=true&url= to view our pdf file using Google Docs . But you will see pop out icon on right side 


 

Alternatively you can use Google Drive link but it also have top header bar which will show in your Web View.

Now what is the Solution ?

via GIPHY

Don't worry In code snippet if you noticed I used some script to load on page load .This do nothing but hide the top bar from Drive and Pop out icon from Google Docs Viewer on page load .

javascript:(function() {document.querySelector('[class=\"ndfHFb-c4YZDc-Wrql6b\"]').remove();})()


Now that all .Your pdf viewer is ready using WebView .Hope you like this Article .If yes Share this post and also Subscribe for more Android related articles.Thank you Once again.😐😐😐

Comments

Popular posts from this blog

How to Download Apk file from Url and Install Programmatically

In this post we learn about download apk file from server or website and then install it Programmatically in Phone. Sometimes we have to download external apk file from server and then install if downloading successfully finished.For this we use AsyncTask class  for background process. So here is Code Snippet for this task.Lets Start :- Before this we have to add these Permissions in Manifest.xml file : <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> DownloadNewVersion.java class DownloadNewVersion extends AsyncTask<String,Integer,Boolean> { @Override protected void onPreExecute() { super.onPreExecute(); bar = new ProgressDialog(getActivity()); bar.setCancelable(false); bar.setMessage("Downl...

Working with Android 11 (Changes and Security Features)

 Hello everyone , I am here with new article which is hot topic nowadays "Android 11" .The stable version of Android.  Android 11 is the eleventh major release and 18th version of Android, the mobile operating system developed by the Open Handset Alliance led by Google. It was released on September 8, 2020.It is comes with many security features and other features as well . And it is now compulsory in play store  to upload new apps with API lavel 30 which is compatible with Android 11 and from November onwards old apps also have to update with API 30 .Some other guidelines you can check out from here . Play Store Guidelines So its clear that we have to update our apps with API level 30 .But Android 11 comes with some changes as well which we have to do in our projects. For example from Android developer site "Android 11 (API level 30) further enhances the platform, giving better protection to app and user data on external storage. ". Scoped storage enforcement: Apps...

How to Disable Edge-to-Edge Mode in Android 15 and Android 16

How to Disable Edge-to-Edge Mode in Android 15 and Android 16 How to Disable Edge-to-Edge Mode in Android 15 and Android 16 With each new Android version, Google continues to enhance UI experiences by embracing modern, immersive design principles. Starting with Android 10, edge-to-edge mode has become the standard, allowing apps to extend their content beneath system bars for a more immersive display. In Android 15 and the upcoming Android 16 releases, edge-to-edge mode remains a core part of the UI framework, leveraging gestures and providing richer visual experiences. However, as developers, there are times when we may want to disable edge-to-edge mode — whether for legacy app compatibility, ensuring content fidelity, or managing complex UI layouts. In this detailed guide, we'll explore how you can disable edge-to-edge mode in Android 15 and Android 16. We will cover the background of edge-to-edge, why you might want to di...