Open PDF Files in Android without WebViews or Intents
By: Doha Kash
Tags: Android App Development, Android Apps, Android, AndroidDev, Programming

Open PDF Files in Android without WebViews or Intents

I’ve been working on a recent project where users receive certificates for their achievements and I wanted them to be able to open these PDF files inside the app without having to be redirected to an outside app that will handle opening the PDF file. So I was searching for an Android Library that could handle this task for me and I actually found a good library that can do just that!

I’ve stumbled across this library “PdfViewPager” through one of Stackoverflow posts where one of the users was recommending it. The good thing about this library is that in addition to being able to load PDF files from assets or SD card, it can also easily load PDF files from Urls which is what I want to show you in this article.

Here is the link to the PdfViewPager library on Github:

Note that the PDFViewPager uses PdfRenderer class, which works only on API 21 or higher.

So in this article, I’m going to create a simple app that will open a pdf file from a FirebaseStorage url.

Add PDFViewPager Library Dependencies

First add the dependencies of this library to your build.gradle file:

Add this line in your app/build.gradle if you are using androidx

implementation 'es.voghdev.pdfviewpager:library:1.1.2'

If you want to use the old android.support instead of androidx , add this dependency

implementation 'es.voghdev.pdfviewpager:library:1.0.6'

Create the xml layout of the MainActivity

Then, create the layout of the main activity as following:

Notice that I’ve added here a LinearLayout that will act as a parent for the PDFViewPager that will be added dynamically in the java code.

Also, don’t forget to add the internet permission in your AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET" />

Modify the MainActivity Java Code

Now in the MainActivity.java, you need to implement the DownloadFile.Listener interface.

We need to implement the interface three methods which are the onSuccess, onFailure, and onProgressUpdate as show below.

Now I will assume that you already have the url of the PDF that you want to open whether it’s from FirebaseStorage or other source. In my case, I want to open the PDF file stored in FirebaseStorage and I have the link so I will declare a global String url variable. I will also declare four other variables as global variables which are the progressBar, the pdfLayout, the remotePDFViewPager, and the pdfPagerAdapter as shown below. Now in the onCreate method, I will assign a value to the url, initialize the progressBar, initialize the pdfLayout, set the progressBar visibility to visible, and create a RemotePDFViewPager object by passing the url, the context, and the activity as a listener.

Now back to the onSuccess method where we need to implement our logic if the pdf download went fine. Here we will initialize the pdfPagerAdapter, setting the adapter to the remotePDFViewPager, updating the layout ,and then hiding the progressBar when all is done. Now in order to update the layout and show the PDF, we will need to add the remotePDFViewPager as a child view to the pdfLayout and set its parameters width and height.

You can also add your own logic on how to handle the failure of the PDF download to the onFailure method which is called when the download fails. The onProgressUpdate method will allow you to show download progress to the user in a determinate way, however we show progress currently through indeterminate progressBar. But feel free to implement you own logic!

Don’t forget to close adapter in onDestroy to release all resources

This is the MainActivity.java full code:

The Final App Preview

And Voila! This is how we open a PDF file from a Url inside our app without using a WebView or an intent.

That’s all for know and have a nice day!