ตัวอย่างนี้แสดงวิธีการแสดงไฟล์ PDF ภายใน WebView ใน Android โดยใช้ Google Docs Viewer เป็นตัวกลางในการเรนเดอร์ไฟล์ PDF
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio ไปที่ File → New Project แล้วกรอกข้อมูลที่จำเป็น
ขั้นตอนที่ 2: เพิ่ม Layout XML
เพิ่มโค้ดใน res/layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
ขั้นตอนที่ 3: เพิ่มโค้ด Java
เพิ่มโค้ดใน src/MainActivity.java:
package com.example.myapplication;
import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading Data...");
progressDialog.setCancelable(false);
WebView web_view = findViewById(R.id.web_view);
web_view.requestFocus();
web_view.getSettings().setJavaScriptEnabled(true);
// URL ของไฟล์ PDF ที่ต้องการแสดง
String myPdfUrl = "gymnasium-wandlitz.de/vplan/vplan.pdf";
// ใช้ Google Docs Viewer เพื่อแสดง PDF
String url = "https://docs.google.com/viewer?embedded=true&url=" + myPdfUrl;
web_view.loadUrl(url);
web_view.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
web_view.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (progress < 100) {
progressDialog.show();
}
if (progress == 100) {
progressDialog.dismiss();
}
}
});
}
}
หมายเหตุ: วิธีนี้ต้องพึ่งพา Google Docs Viewer ซึ่งอาจมีข้อจำกัด เช่น ไฟล์ PDF ต้องเข้าถึงได้จากสาธารณะ และอาจไม่ทำงานหาก Google เปลี่ยน API ในอนาคต สำหรับแอปพลิเคชันจริง แนะนำให้ใช้ไลบรารี PDF แบบ Native เช่น
PDFView หรือ PdfRenderer (API 21+)
ขั้นตอนที่ 4: กำหนดสิทธิ์ใน AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
การทดสอบ
รันแอปพลิเคชันบนอุปกรณ์จริง ผลลัพธ์จะแสดงไฟล์ PDF ผ่าน Google Docs Viewer
