Tuesday 7 May 2013

Android view PDF in Webview

Hi all,

Today we are going to see about how to view pdf files in android using google docs.

Normally Android don't have support to view the PDF files. So that we should use web view to view the PDF files in Android.

This is not much different from web view. just we need to append the Google docs URL in web view.loadurl().

1. main.xml
below is the layout code with web view.

<?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"
    >
   
    <WebView 
        android:id="@+id/webview_compontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1.0"  
    />
</LinearLayout>

2. Your Activity code should be like below 
 package org.example.webviewdemo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewDemo extends Activity {      
  

    private WebView webView;
    Activity activity ; // instead of context we can use activity
    private ProgressDialog progDailog;  // loader
    String GoogleDocs="http://docs.google.com/gview?embedded=true&url="; 

                                     // google docs support url.
 
       @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        activity = this;      
        progDailog = ProgressDialog.show(activity, "Loading","Please wait...", true);
        progDailog.setCancelable(false);      
       webView = (WebView) findViewById(R.id.webview_compontent);     
      
   
       webView.getSettings().setJavaScriptEnabled(true);   
       webView.getSettings().setLoadWithOverviewMode(true);
       webView.getSettings().setUseWideViewPort(true);      

//following lines are to show the loader untile downloading the pdf file for view.
        webView.setWebViewClient(new WebViewClient(){
          
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                progDailog.show();
                view.loadUrl(url);
           
                return true;              
            }
            @Override
            public void onPageFinished(WebView view, final String url) {
                progDailog.dismiss();
            }
        });
      
        webView.loadUrl(GoogleDocs+"http://178.239.16.28/fzs/sites/default/files/dokumenti-vijesti/sample.pdf");   // webview loader to load the URL of file

 }  }


Don't forget to add below permission in manifest file.

  <uses-permission android:name="android.permission.INTERNET" />


happy coding.

 


3 comments:

Web Helper said...

Dear sir,

It helps me a lot but what if i want to download pdf file to my mobile phone. There is an option of Downloading but when i click on that link, it just refreshes that page.

Please give me solution for downloading pdf file to my mobile.
Wating for your reply.


Ashish Rawat

Anonymous said...

Too many errors in this code. I can't get out of it.
Very sorry, 'cause is just what I was looking for :-(

Unknown said...

This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.Android Training institute in chennai with placement | Android Training in chennai