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.
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
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.
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); } }); } }
Thank you again! Your code here and in stackoverflow really helped me.
ReplyDeleteThank you very very much.
ReplyDeleteThank you very very very much
ReplyDeleteIt gives no preview available for me
ReplyDeleteIt gives no preview available for me
ReplyDeletesame problem
Deletesame problem
Deletesame problem
DeleteHello If I want to open pdf links of any website. How could I do it?
ReplyDeletethanks
ReplyDeletethanks very much
ReplyDeleteit give scrolling issue
ReplyDeleteThis tut only can view pdf..however the pdf cannot download direct into the phone...is there a way to do that?
ReplyDeleteits automatically go out side of code and open what to do
ReplyDeleteno preview help pls
ReplyDeleteIt is not working in 2021. I am also developer
ReplyDelete