Rete #adessonews

Cerca e consulta articoli.

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
#adessonews
Landing
Nostri servizi
Traslocchi

Finanziamenti - Agevolazioni

Siamo operativi in tutta Italia

How to open a PDF file in Android programmatically?


How to open a PDF file in Android programmatically?

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:

  1. Project setup
  2. Ways of opening PDF in Android
  3. Making UI and adding Activities for the project
  4. Opening a PDF file using WebView
  5. Opening a PDF file using AndoirdPdfviewer library
  6. Project source code and What next?

Project setup

In this tutorial, we will make a project and try various ways of opening PDF file, Here we are going to set up our project:

Finanziamenti - Agevolazioni

Siamo operativi in tutta Italia

 

  • Start a new Android Studio Project
  • Select Empty Activity and Next
  • Name: Open-PDF-File-Android-Example
  • Package name: com.mindorks.example.openpdffile
  • Language: Kotlin
  • Finish
  • Your starting project is ready now
  • Under your root directory, create a package named utils.(right-click on root directory > new > package)
  • In the utils package, create one object classes: FileUtils.(right-click on utils > new > Kotlin file/class > Object class)

If you are preparing for your next Android Interview, Join our Android Professional Course to learn the latest in Android and land job at top tech companies.

Ways of opening PDF in Android

If 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:

  1. From Assets: Let’s take an example, if you want to display some icons in your application then you will put all your icons in the drawable folder and then you will use those icons in your application. Same is with the case of PDF files also. If you have some PDF file that is constant and you want to display it in your application then you can put that PDF file in the assets folder and use that PDF in your app. One example can be the Terms and Conditions file. The terms and conditions files are rarely changed. So, you can put that document in the assets folder and use it.
  2. From Device: The other way of opening a PDF is to open it from the device itself. Here, you can open the PDF files present in your mobile device. This is the most used approach for opening the PDF in an Android device.
  3. From the Internet: Here, you can open PDF files from the internet. All you need to do is just use the URL of the PDF file and after downloading the PDF file, you can open the PDF file in your mobile application.

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 project

In our example, we are going to cover four different cases:

  1. Opening a PDF file using WebView
  2. Opening a PDF file from assets using AndroidPdfViewer library
  3. Opening a PDF file form storage using AndroidPdfViewer library
  4. Opening a PDF file from the internet using AndroidPdfViewer library

So, for the first point, we will use WebViewActivity and for 2nd, 3rd, and 4th point, we will be using PdfViewActivtiy.

Finanziamenti - Agevolazioni

Siamo operativi in tutta Italia

 

Create two activities named WebViewActivtiy and PdfViewActivity.(right-click on root directory > new > Activity> Empty Activity)

Now, for the above four actions, create four buttons and assign the task to open activity by those four buttons.

The code for the activity_main.xml file is:

Finanziamenti - Agevolazioni

Siamo operativi in tutta Italia

La rete Adessonews è un aggregatore di news e replica gli articoli senza fini di lucro ma con finalità di critica, discussione od insegnamento,

come previsto dall’art. 70 legge sul diritto d’autore e art. 41 della costituzione Italiana. Al termine di ciascun articolo è indicata la provenienza dell'articolo.

Per richiedere la rimozione dell'articolo clicca qui

Open chat
1
Ciao posso aiutarti?
Finanziamenti e agevolazioni personali e aziendali.
Utilizza questa chat per richiedere informazioni o l'attivazione di un finanziamento e/o agevolazione.
%d blogger hanno fatto clic su Mi Piace per questo: