PDF List Example

This example shows you how show list of all pdf file saved on your sdcard and open the selected file to view via pdfviewer..

Algorithm:

1.) Create a new project by File-> New -> Android Project name it PDFListExample.
2.) You will see some default code into your main.xml and android manifest file.
3.) Download and install any adobe reader application from android market.
4.) Make sure u have some pdf files on your sdcard (simulator or device).
5.) Write following into main.xml file:

 

<!--?xml version="1.0" encoding="utf-8"?-->
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_height="fill_parent" android:drawSelectorOnTop="false" />

 

 

6.) Run for output.

Steps:

1.) Create a project named PDFListExample and set the information as stated in the image.

Build Target: Android 2.2
Application Name: PDFListExample
Package Name: com.pdftest
Activity Name: PDFListrActivity
Min SDK Version: 8

2.) Open PDFListActivity.java file and write following code there:

 

package com.pdftest;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class PDFListActivity extends ListActivity {
ArrayAdapter adapter;
int clickCounter=0;
ArrayList listItems=new ArrayList();
private File[] imagelist;
String[] pdflist;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

File images = Environment.getExternalStorageDirectory();
imagelist = images.listFiles(new FilenameFilter(){
public boolean accept(File dir, String name)
{
return ((name.endsWith(".pdf")));
}
});
pdflist = new String[imagelist.length];
for(int i = 0;i<imagelist.length;i++)
{
pdflist[i] = imagelist[i].getName();
}
this.setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, pdflist));
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
PackageManager packageManager = getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() > 0 && imagelist[(int) id].isFile()) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(imagelist[(int) id].getAbsoluteFile());
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
}

}
}

 

 

3.) Compile and build the project.
4.) Make sure you have installed pdf viewer application on your device or simulator and you must have pdf files on your sdcard as shown in below image otherwise output will be a blank screen.

Output

Leave a Comment:

14 comments
siddhi says December 3, 2011

i want code for displaying action bar now.if possible mail me now itself

thanks

Reply
Pramod says December 3, 2011

thts very helpful for me

Reply
Deepa says December 3, 2011

Nice topic!!!
Sir i want the code how to create a own database which save in box messages…
looking forward for Ur answer….

Reply
Raghvendra Kr. Tiwari says December 4, 2011

Thanks sir for this…..Sir i want to know how the connect couchdb database with our program….

Reply
harikrishna says December 10, 2011

code

Reply
Girish says December 26, 2011

Hi,

I want to know , ‘How to create or delete directory in sd card in android’.
Please let me know source code with example.

Thanks & Regards,
Girish

Reply
http://www.c4learn.com/ says January 11, 2012

this is best article.. thanks a lot.. i am new person to android development.. i hope this site will give me lot of knowledge.. nice representation .. 🙂 🙂

Reply
Sree says August 2, 2012

Hi Sushant,
I am working on the above code. It seems to only view PDF file which is not yet saved in sdcard or downloaded. Can you please suggest a code which will be downloading 3 or more .pdf file and listing it in a listview?

Reply
chandar rao says August 7, 2012

thank you for your stuff.

Reply
golfpsy101 says February 9, 2013

Thanks for the example code. I got it to work for the files i wanted however the list is not sorted alphabetically. I have been not been able to sort it properly. Any suggestions?

Reply
golfpsy101 says February 9, 2013

Nevermind, I was able to resolve the issue. I added Arrays.sort(imagelist) and Arrays.sort(pdflist) as seen below:

pdflist = new String[imagelist.length];
for (int i = 0; i < imagelist.length; i++) {
pdflist[i] = imagelist[i].getName(); }

Arrays.sort(imagelist);
Arrays.sort(pdflist);

this.setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, pdflist));

Reply
Parth Dani says March 15, 2013

Sir Thanks for the gr8 Tutorial….
Sir Now I am trying to convert Pdf to Image but I am not able to do so.
Can you please provide me with the Tutorial for converting Pdf files into Image files in Android.

Reply
Alex Ganner says April 9, 2013

Attractive portion of content. I simply stumbled upon your website and in accession capital to say that I acquire actually enjoyed account your weblog posts. Anyway I’ll be subscribing for your augment or even I achievement you get entry to persistently fast.

Reply
ian says July 21, 2013

why does it has a problem like “Unfortunately, PDFListExample has stopped.” ? How can I fixed it?

Reply
Add Your Reply