Opening and viewing documents in Android applications are very interesting and a must to have in every application. You can open any application present on your mobile and you will find that every application contains documents in some or the other way. And among these documents, the most popular and widely used document format is the PDF format. PDF or Portable Document Format is a file format that has captured all the elements of a printed document. This is the most used document format. For example, in the Paytm application, you get your monthly expenses in the form of PDF document. So, if you also want to display some kind of document in your application, then you can open this PDF format document. So, welcome to MindOrks and in this tutorial, we will learn how to open a PDF file in Android programmatically. We will cover the below topics in this tutorial:
Project setupIn this tutorial, we will make a project and try various ways of opening PDF file, Here we are going to set up our project:
Ways of opening PDF in AndroidIf you want to display PDF in your Android application, there are various ways of doing it. Some of the ways of opening the PDF can be:
So, we will look upon all these ways of viewing the PDF in your Android Application. Let’s make the UI of the project. Making UI for the projectIn our example, we are going to cover four different cases:
So, for the first point, we will use Create two activities named Now, for the above four actions, create four buttons and assign the task to open activity by those four buttons. The code for the
In the
We are done with the UI part. Let's learn how to view PDF from WebView. Opening a PDF file in Android using WebViewThe very first and the easiest way of displaying the PDF file is to display it in the WebView. All you need to do is just put WebView in your layout and load the desired URL by using the So, add a WebView in the
Now, open the
Now, all we need to do is open the above URL in the WebView by calling the
The last thing that you need to do is adding
Now, run the application on your mobile phone and the PDF will be displayed on the screen. Note: The opening of PDF in WebView depends on your internet speed, so wait for sometimes if your internet is slow. Opening a PDF file in Android using AndroidPdfViewer libraryThere are various libraries that can be used to display PDF files in our application. In our tutorial, we will learn how to open a PDF
file from Assets, Phone Storage, and from the Internet by using the Also, we will be using Adding dependencies and permissions Open the app level
Since we will be reading pdf from INTERNET. Open the AndoidManifest.xml file and add the below:
Adding PDFView The AndroiPdfViewer provides a PDFView to display PDF files in it. So, write the below code in
We can use the AndroidPdfViewer to open the PDF from:
So, we need to write the code to connect the button click of
Call the above method from the
Let's learn how to display PDF from assets, storage, and internet. Assets FolderFirstly, we will look upon how to view PDF, stored in the Assets Folder. Creating an assets folder Create an assets folder by right-clicking on main > New Folder > Assets Folder and paste the PDF document into it. PDF file name: MindOrks_Android_Online_Professional_Course-Syllabus.pdf Create Create a method named
Now, in the
Call the above method from the
Finally, run the application on your mobile device and see the output. From Phone StorageNow,
we will look upon how to open PDF files from the Phone’s storage. So, we have to launch an intent to find the file having PDF format and the selected file will be displayed in the Create a function
Once, the user selects a PDF, the
Now, create a method named
Now, you can check the output by running your application on your mobile device and select the desired PDF. PDF from InternetLastly, our aim is to view the PDF files from the Internet. We will first download the PDF by using the So, we need to download the file first by using the PRDownloader library. Initialise
it in the
Now, you need to download the file from the INTERNET by using the
The
Finally, call the
Now, call the
Finally, run the application and try to verify all the three options i.e. assets, storage and internet options to view PDF in Android Application. Try to replace the PDF link used in the above example with your own PDF URL. There are many other methods present in the AndroidPdfViewer library. You can explore all the methods from here. Project source code and What next?You can get the source code of the whole project from here. You can find more open-source project by MindOrks from here. Have a look at our Interview Kit for company preparation. Do share this tutorial with your fellow developers to spread the knowledge. You can read more blogs on Android on our blogging website. Apply Now: MindOrks Android Online Course and Learn Advanced Android Happy Learning :) Team MindOrks! |