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

Popular posts from this blog

Python Turtle Example: Flappy Bird Development in under 10 mins!

Python Turtle Example | Develop Flappy Bird Clone in 10 mins In the following tutorial, we have used following Python libraries:
1. random: To generate obstacles at random position. 2. turtle: To help provide directions to the player. 3. vector from freegames: To 2-dimensional vectors.
Before developing the game, here is a screenshot of what you'll be developing in 10 mins.
Steps to create Python Flappy Bird Game:
1. Open Pycharm and install 'freegames' package by going to Files > Settings > Project Interpreter. 2. Click on '+' sign on right and search for 'freegames' package. 3. Import packages in your project by writing following lines of code:
from random import * from turtle import * from freegames import vector 4. There are two variables required in the game, one for Bird object and another for an array of obstacles:
bird = vector(0, 0) balls = []
5. Create function for the player response on screen tap:
def tap(x, y): "Move bird up in res…

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…