Deepshikha Puri, the young Indian Entrepreneur heading the mobile development trade from years to successive extent, has worked with numerous clients and many tremendous brands in this industry of mobile encompassing in India and overseas maintaining promising work relationships with each of them with an impression to manage it's whole thing.

Monday, July 17, 2017

Open Pdf from url in Android Programmatically

Video Demo:



You can easily view your online pdf file from url in webview. To load the pdf file from url you need to use the google drive link. Download source code from here.

android open pdf file programmatically


activity_main.xml

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <WebView
        android:layout_width="match_parent"
        android:id="@+id/webview"
        android:layout_height="match_parent">

    </WebView>

    <ProgressBar
        android:layout_width="wrap_content"
        android:id="@+id/progressbar"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content" />


</RelativeLayout>


MainActivity.java


package com.deepshikha.openpdf;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {
    WebView webview;
    ProgressBar progressbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webview = (WebView)findViewById(R.id.webview);
        progressbar = (ProgressBar) findViewById(R.id.progressbar);
        webview.getSettings().setJavaScriptEnabled(true);
        String filename ="http://www3.nd.edu/~cpoellab/teaching/cse40816/android_tutorial.pdf";
        webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + filename);

        webview.setWebViewClient(new WebViewClient() {

            public void onPageFinished(WebView view, String url) {
                // do your stuff here
                progressbar.setVisibility(View.GONE);
            }
        });

    }
}

16 comments:

  1. Thank you again! Your code here and in stackoverflow really helped me.

    ReplyDelete
  2. It gives no preview available for me

    ReplyDelete
  3. It gives no preview available for me

    ReplyDelete
  4. Hello If I want to open pdf links of any website. How could I do it?

    ReplyDelete
  5. This tut only can view pdf..however the pdf cannot download direct into the phone...is there a way to do that?

    ReplyDelete
  6. its automatically go out side of code and open what to do

    ReplyDelete
  7. It is not working in 2021. I am also developer

    ReplyDelete