Skip to main content

How To Open PDF File in Android Application Example Included

Want to know How To Open PDF File in Android Application Example. To open a PDF File in Android Application, your app can take help from Free Android library available on Github.

Here are few apps made from PDF Library you can look at, click here to know more.

By using Android Library you are not only leveraging the hard work from another developer but also making it easier and quicker to develop an android app.

How To Open PDF File in Android Application Example 

How To Open PDF File in Android Application Example
How To Open PDF File in Android Application Example

In this tutorial series, we will teach you how to open a pdf file in Android Application Example included with the help of Android PDFViewer library developed by Barteksc.



Before I proceed further, here is the screenshot of the kind of PDF Viewer that’ll be created using Android Studio.

How To Open PDF File in Android Application Example

 How To Open PDF File in Android Application Example


 How To Open PDF File in Android Application Example

The app will open a specified pdf file which will be copied in the assets folder of your Android studio project. I've used bitcoin.pdf file, you can use any of your choices.

Software Files used:

  1. Android Studio (Version 3.x)
  2. Any 1 PDF File

Files Edited / Created:

  1. activity_main.xml
  2. MainActivity.Java
  3. Build.Gradle (app)

Here are the Steps you need to follow on 'How To Open PDF File in Android Application Example':

1.Open Android Studio and create a new project with Application name as, ‘FIMC PDFViewer’ (you can use any of your choices!).

2. We have used API level 19 in our project as 90% devices support this, click next and select to create an Empty Activity.

3. Once Gradle build is completed, go to 'Gradle Scripts' in the left side panel of Project Window. Search for 'build.gradle (app)

4. Search for dependencies and add the following line of code to it.

dependencies {
   
    compile 'com.github.barteksc:android-pdf-viewer:2.8.2'
} 

5. To make sure that you\'re using the updated version of PDFViewer library, I strongly recommend visiting source code on GitHub

6. Now check for a Yellow color bar on top of Project Window and click on 'Build Gradle'.

7. Now go to activity_main.xml file in res > layout in Text mode and remove any component other than the layout.

Now add following lines of code to it:

<com.github.barteksc.pdfviewer.PDFView    android:id="@+id/pdfView"    android:layout_width="match_parent"    android:layout_height="match_parent" /> 

You can view the complete code of  activity_main.xml here:

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.faultinmycode.fimcpdfviewer.MainActivity">

    <com.github.barteksc.pdfviewer.PDFView        android:id="@+id/pdfView"        android:layout_width="match_parent"        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout> 

8. Go back to Project panel in the left of Android Studio and right click on the 'app' folder.

9. Go to New Folder > New Assets Folder. Click OK. New assets folder is now created.

10. Right-click on the \'assets\' folder and select, 'Open in Explorer'.

11. Copy and Paste your PDF File in this folder.

12. Go and open MainActivity.Java file in java folder. Using findViewByID function link the PDFView component and then link it to the pdf file in assets folder using the code as shown below:

PDFView pdfView = (PDFView) findViewById(R.id.pdfView);
pdfView.fromAsset("bitcoin.pdf").load(); 

You can view the complete code of \'MainActivity.java here:

package com.faultinmycode.fimcpdfviewer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.github.barteksc.pdfviewer.PDFView;

public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PDFView pdfView = (PDFView) findViewById(R.id.pdfView);
        pdfView.fromAsset("bitcoin.pdf").load();
        
    }
} 

Now that your own personal PDFViewer App is ready, Build and run in on your Device. Let us know how did you like our 'How To Open PDF File in Android Application Example' in the comment section below .

Comments

  1. app size is 20mb, is possible to compress app size

    ReplyDelete
  2. Thanks for the lesson. Can you show us how to print from PDFView, after open? and do we have solution how to add data from Java code to PDFView. Thanks!

    ReplyDelete

Post a Comment

Popular posts from this blog

Open Weather Map API Example | Create Android Weather App

In this tutorial blog, using open weather map API example, we will create an Android weather app. The Android App will able to access Open weather map website server and access weather details of particular location.

The App will look similar to the image shown below. It will fetch real time weather of the location provided in MainActivity.Java file.


Software / Files used:Android Studio (Version 3.x) Files Edited / Created:activity_main.xmlMainActivity.JavaWeather.JavaAndroidManifest.xml Steps Involved in Open Weather Map API Example: Here are the Steps you need to follow on ‘Open Weather map API Example’ to create Android Weather App:

1. Open the Open Weather map API link provided here. Sign up for Current Weather Data API key here.

2. Once the key is received, save it to notepad or on stick notes.

3. Open Android Studio and create a new Project with name, ‘FIMC Weather App’, we will select an empty activity in the wizard and wait for Gradle build.



4. Open AndroidManifest.xml…

Android Live Wallpaper Tutorial (using Android Studio 3.x) | FaultInMyCode

The Android Live Wallpapers are the way to redesign your mobile background into something amazing animated GIF using different effects and motions. Many novice developers are making good money by Henceforth here is Android Live Wallpaper tutorial for you to build your own Live wallpaper and start earning today as a developer!



This tutorial is a part of our Blog series on ‘Roadway to Android Development Course’, its suitable for Beginners to programming as well as the android enthusiast to learn about Android development and build their own apps.

In this tutorial for beginners, you’ll learn how by using Android Studio 3.0 and by having basic knowledge of Java, we will teach you to build your own Live wallpaper for Android which you may later publish in the Google Play Store. We assume you have some experience with Android studio, if not please check how to build your own calculator app and how to create simple login app in Android Studio 3.x tutorial.
How to build Android …