এই উদাহরণটি প্রদর্শন করে কীভাবে অ্যান্ড্রয়েডে কোটলিন ব্যবহার করে WebView-এ কাস্টম ফন্ট ফেস পরিবর্তন করা যায়।
পদক্ষেপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে যান File → New Project এবং প্রজেক্ট তৈরি করার জন্য প্রয়োজনীয় সব বিবরণ পূরণ করুন।
পদক্ষেপ ২: 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>
পদক্ষেপ ৩: অ্যাসেট ফোল্ডার তৈরি করুন এবং ফাইল যোগ করুন
- Right-click
app→New→Folder→Assets Folder - Assets ফোল্ডারে রাইট-ক্লিক →
New→File→webView.html - আপনার কাস্টম ফন্ট ফাইল (যেমন
Font.otf) assets ফোল্ডারে কপি করুন
webView.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">
"All our dreams can come true, if we have the courage to pursue them." – Walt Disney.
</body>
</html>
পদক্ষেপ ৪: src/MainActivity.kt-এ কোড যোগ করুন
import android.os.Bundle
import android.webkit.WebView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val webView = findViewById<WebView>(R.id.webView)
webView.settings.javaScriptEnabled = true
webView.loadUrl("file:///android_asset/webView.html")
}
}
পদক্ষেপ ৫: androidManifest.xml-এ কোড যোগ করুন
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.q11">
<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>
অ্যাপ্লিকেশন চালান
আপনার আসল অ্যান্ড্রয়েড ডিভাইস কম্পিউটারের সাথে সংযুক্ত আছে বলে ধরে। Android Studio-তে আপনার প্রজেক্টের এক্টিভিটি ফাইল খুলুন এবং টুলবারের রান আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস সিলেক্ট করুন এবং ডিভাইসে ডিফল্ট স্ক্রিন দেখুন।