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:
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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+android:id/list"android:layout_width="fill_parent"
android:layout_height="fill_parent"android:drawSelectorOnTop="false" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+android:id/list"android:layout_width="fill_parent"
android:layout_height="fill_parent"android:drawSelectorOnTop="false" />
</LinearLayout>
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
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 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;
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<String> adapter;
int clickCounter=0;
ArrayList<String> listItems=new ArrayList<String>();
private File[] imagelist;
String[] pdflist;
ArrayAdapter<String> adapter;
int clickCounter=0;
ArrayList<String> listItems=new ArrayList<String>();
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);
@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<String>(this,
android.R.layout.simple_list_item_1, pdflist));
}
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<String>(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);
}
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.
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
No comments:
Post a Comment