Load PDF in Android WebView
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:
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.
🎥 Load PDF in Android using WebView - #AndroidStudio (YouTube Video)
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.
Related Posts
Google Volley Android Studio Tutorial
Oct 01, 2024
WebView In Android
May 24, 2022
Razorpay Integration in Android
May 19, 2022
Comments (0)
Leave a Comment
No comments yet. Be the first to comment!