WebView में कस्टम फॉन्ट लोड करने के लिए एसेट्स फोल्डर में HTML/CSS और फॉन्ट फ़ाइल रखने का तरीका।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट में WebView
res/layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
tools:context=".MainActivity">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
चरण 3: एसेट्स में HTML और फॉन्ट
assets फोल्डर बनाएं: Project व्यू में main पर राइट-क्लिक → New → Folder → Assets Folder
assets/webView1.html:
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>WebView Custom Font</title>
<meta forua="true" http-equiv="Cache-Control" content="max-age=0"/>
<style type="text/css">
@font-face {
font-family: 'CustomFont';
src: url("file:///android_asset/Font.otf");
}
body {
font-family: 'CustomFont', serif;
font-size: medium;
text-align: justify;
color: #ffffff;
}
</style>
</head>
<body style="background-color:#212121">
This file was loaded from android assets folder using webView.
</body>
</html>
assets/Font.otf: अपनी .otf या .ttf फॉन्ट फ़ाइल यहां रखें
चरण 4: MainActivity
src/MainActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView.loadUrl("file:///android_asset/webView1.html");
}
}
चरण 5: मैनिफेस्ट
मानक मैनिफेस्ट (इंटरनेट अनुमति की आवश्यकता नहीं क्योंकि लोकल फ़ाइल लोड हो रही है)।
महत्वपूर्ण नोट्स
file:///android_asset/- एसेट्स फोल्डर एक्सेस करने का विशेष URI स्कीम- फॉन्ट फ़ाइल नाम HTML में केस-सेंसिटिव है
- बड़े फॉन्ट के लिए WebView सेटिंग्स में
setJavaScriptEnabled(true)सक्षम करें - API 21+ के लिए
WebView.setRendererPriorityPolicy()पर विचार करें