A Simple PDF Picker

If you have followed the previous Android Hello World example, at this stage you should feel comfortable with creating Android Studio projects from the very start.

Let’s briefly recap. We’ve looked at how to import and compile the plugpdf module, and we already know to instantiate a ReaderView object in order to display a simple native PDF viewer.

A Simple PDF Picker

So let’s keep moving forward and take a small step in complexity. In contrast with yesterday’s Hello World, where the eBook “Gone With the Wind” is automatically displayed once the app is launched, today’s sample project displays a file chooser so that the end user can conveniently pick any PDF file from their SD card.

Using a Third-Party File Manager

For this exercise we recommend you use any of the following third party file managers available at Google Play.

  • ES File Explorer File Manager
  • ASTRO File Manager with Cloud
  • FX File Explorer
  • File Manager (Explorer)

Well, in fact you are not bound to use those four only; you may be already using your favorite one (not included in the list) which is ok. The important thing is that file managers usually come with advanced features and can help to solve a number of different cross-platform issues.

For your information, we have run today’s Simple PDF Picker on a real device (LG Spirit with Lollipop pre-installed) and everything has gone ok with all the file explorers listed above.

Creating a New Project and Importing the PlugPDF SDK

Let’s create a new project called SimplePDFPicker. This is very easy to do, so if you have any questions please read the Hello World tutorial first.

We must end up with a project anatomy like this:

Figure 1. The new project and the plugpdf module are already created and imported

Figure 1. The new project and the plugpdf module are already created and imported, respectively

Customizing the Main Activity

MainActivity.java file for the user to choose PDF files from his/her SD card.

Updating the XML Files

manifests/AndroidManifest.xml file:

Notice that every time a runtime change takes place, for example the device screen orientation varies from laptop to portrait or viceversa, the main activity’s onCreate method is run. This default behavior is well documented on section Handling Runtime Changes of the Android Developers website.

In our case, since we don’t want to update resources every time the screen orientation, keyboard availability and screen size are changed, the following declaration must be written in our manifest.

res/layout/activity_main.xml file:

Running the PDF Picker

Figure 2. Very simple GUI created from the activity_main.xml layout

Figure 2. Very simple GUI created from the activity_main.xml layout

Figure 3. The user is asked to pick a file

Figure 3. The user is asked to pick a file from his/her SD card

The app displays the PDF

Figure 4. The app displays the PDF document selected by the user

What’s the Code Doing?

In the first place, MainActivity loads the XML layout file from its onCreate method.

And from that moment on, the Android application awaits a response from the user.

As you see, when the Select a PDF File button is clicked the app displays a file chooser. This is possible through Android’s Intents and Standard Activity Actions. Then, the onActivityResult method is executed once the PDF file has been selected from the SD card.

The main activity’s onActivityResult method is responsible for initiating the PDF viewer.

Since the user can choose a file from any folder (and therefore the programmer has to deal with the file system), the openFile method of the ReaderView class is used on this occasion.

Otherwise, recall that in our previous Hello World example we created the readAsset method so that the files located in the assets folder could be dynamically read by ReaderView‘s openData method.

Congratulations! That’s all for today. We have just written a few lines of Java code, and now we deserve a little break.

2 replies
    • jordi
      jordi says:

      Hello octa,

      Can you please be more specific about your issue?

      Please provide some details, we will be happy to assist you in the Support Forum.

      Best regards,

      Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *