Saturday, September 8, 2012

DF List In Andoid

PDF List ExampleThis example shows you how show list of all pdf file saved on your sdcard and open the selected file to view via pdfviewer..
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"?>
<LinearLayout xmlns:android=""
        android:orientation="vertical" android:layout_width="fill_parent"

        <ListView android:id="@+android:id/list"android:layout_width="fill_parent"
                android:layout_height="fill_parent"android:drawSelectorOnTop="false" />
6.) Run for output.
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 file and write following code there:
package com.pdftest;
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
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;
        /** Called when the activity is first created. */
        public void onCreate(Bundle savedInstanceState) {
                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));
        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);
                 List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
                 if (list.size() > 0 && imagelist[(int) id].isFile()) {
                     Intent intent = new Intent();
                     Uri uri = Uri.fromFile(imagelist[(int)id].getAbsoluteFile());
                     intent.setDataAndType(uri, "application/pdf");
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.

No comments:

Post a Comment