WebView-এ কাস্টম ফন্ট লোড করা
এই টিউটোরিয়ালে Assets ফোল্ডার থেকে কাস্টম ফন্ট ফাইল (.otf/.ttf) লোড করে WebView-এ দেখানোর পদ্ধতি ব্যাখ্যা করা হয়েছে।
ধাপ ১: প্রজেক্ট তৈরি
Android Studio-তে নতুন প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট (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>
ধাপ ৩: Assets ফোল্ডার ও ফাইল তৈরি
- Project 뷰-এ
mainরাইট-ক্লিক → New → Folder → Assets Folder - assets ফোল্ডারে রাইট-ক্লিক → New → File →
webView1.html - assets ফোল্ডারে আপনার ফন্ট ফাইল কপি করুন (যেমন:
Font.otf)
webView1.html কন্টেন্ট:
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>Custom Font WebView</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>
ধাপ ৪: 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.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/webView1.html");
}
}
ধাপ ৫: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.webviewfont">
<application ...>
<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>
গুরুত্বপূর্ণ নোটস
file:///android_asset/- এসেট ফোল্ডার অ্যাক্সেসের স্পেশাল URI স্কিম- ফন্ট ফাইলের নাম HTML-এ ঠিকমতো লিখুন (কেস-সেনসিটিভ)
- WebView-এ জাভাস্ক্রিপ্ট অন করুন:
setJavaScriptEnabled(true) - সাপোর্টেড ফরম্যাট:
.ttf,.otf,.woff(API 21+)
আধুনিক বিকল্প: WebFont Loader / Google Fonts
অনলাইন থাকলে Google Fonts CDN ব্যবহার করুন:
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
<style> body { font-family: 'Roboto', sans-serif; } </style>
সীমাবদ্ধতা: অফলাইন অ্যাপের জন্য অ্যাসেট-ভিত্তিক ফন্টই একমাত্র উপায়। বড় ফন্ট ফাইল অ্যাপ সাইজ বাড়াবে।