Android PDFViewer Sample


Android PDFViewer is a strong application for reading .pdf files stored in SD cards of the device. The following article demonstrates a simple android pdfviewer application for android 4.0 helps to read .pdf files from internet in android device in 3G, Wi-Fi network.

To build the application , Click File – > New Project in Eclipse for Android IDE.

  • Put the name of the project & select Target device as Android 4.0
  • Now put the following code in Main.xml

<?xmlversion=“1.0”encoding=“utf-8”?>

<LinearLayoutxmlns:android=http://schemas.android.com/apk/res/android&#8221;

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

android:orientation=“vertical”>

<ListViewandroid:id=“@+android:id/list”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

android:drawSelectorOnTop=“false”/>

</LinearLayout>

 

Now, Modify the PDFListExampleAcitvity.java file as follows: 

package com.example.pdflistexample;

import android.app.Activity;

import android.os.Bundle;

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.Environment;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.ListView;

public class PDFListExampleActivity 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. */

@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));

}

@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);

}

}

}

  • Before running the application , make sure that you have downloaded some .pdf files in SD Card or installed pdfviewer in the device.
  • Otherwise download from Android Marketplace & test the app..

 

 

 

Advertisements

About Anindita
Anindita Basak is working as Big Data Cloud Consultant in Microsoft. Worked in multiple MNCs as Developer & Senior Developer on Microsoft Azure, Data Platform, IoT & BI , Data Visualization, Data warehousing & ETL & of course in Hadoop platform.She played both as FTE & v- employee in Azure platform teams of Microsoft.Passionate about .NET , Java, Python & Data Science. She is also an active Big Data & Cloud Trainer & would love share her experience in IT Training Industry. She is an author, forum contributor, blogger & technical reviewer of various books on Big Data Hadoop, HDInsight, IoT & Data Science, SQL Server PDW & PowerBI.

3 Responses to Android PDFViewer Sample

  1. Pingback: Android PDFViewer Sample « Anindita's Blog | Apple Bosom

  2. Pingback: Buy Targeted Facebook Fans

  3. Pingback: Buy Facebook Fans

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: