We can load PDF in android easily using WebView. We will be using the simplest way for displaying PDF file in android. Using the Google Docs PDF viewer we could do this easily. Now lets get in to the implementation and source code.
Related Posts:
- How to implement Splash Screen the right way
- Set up Download manager in WebView
- Google Volley Android Implementation
You could watch the video, it explained everything and also shows you how exactly you can Load PDF in Android WebView. Don’t forget to subscribe if you find out contents useful.
The first thing you have to do is implement WebView, its really easy to implement a WebView in android. We will be doing only the necessary part.
Designing the layout part is the first thing, we will be using a Linear Layout as parent layout and then will be using the WebView inside it. You should set an id for the WebView and this is important for initializing the WebView.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WebActivity">
<WebView
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Go to the Java part and create an object for WebView then initialize it.
WebView webView = findViewById(R.id.web);
Now we have to set the WebView client.
webView.setWebViewClient(new WebViewClient());
If you like to have zoom and need to enable JavaScript for WebView just these two lines which does this.
webView.getSettings().setSupportZoom(true);
webView.getSettings().setJavaScriptEnabled(true);
You can load the PDF file now, you have to use the Google docs PDF viewer for this. Just add “https://docs.google.com/gview?embedded=true&url=” + the URL of PDF file.
webView.loadUrl("https://docs.google.com/gview?embedded=true&url=" + "https://pdfurl.com/file.pdf");
Full source code to Load PDF in Android WebView
WebView webView = findViewById(R.id.web);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setSupportZoom(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://docs.google.com/gview?embedded=true&url=" + "https://pdfurl.com/file.pdf");
That’s all you can load a PDF file in WebView these simple steps.
Hello, I need to load PDF file and zoom it in same url can you please help me to do this?
Check the video we have shown that too.
Hello Sir
Please help me with the Source Code So that I should Just Directly Change the Pdf url
You can easily download the source code files from https://youtube.codeseasy.com/2021/load-pdf-in-webview
When I try to access the pdf that is present in my google drive it failed to load , showing blank screen .How can I fix it ?
Use a .pdf link. If you are using google drive link just give that link only don’t include the https://docs.google.com/gview?embedded=true&url= link. Just give the drive link alone. That too the link to video the pdf.
Hii
is it possible to load web view pdf from internal/sd card storage without using the google docs link directly load to get external storage
This is working after i have added these lines in webview
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
yes
hi i am trying to get a pdf i stored on firebase storage to display in a web view. show me.