পূর্ব প্রস্তুতি
এই উদাহরণটি দেখায় কিভাবে কাস্টম ফন্ট ফাইল (`.ttf`) ব্যবহার করে TextView-এ Font Awesome আইকন বা স্টাইলিশ টেক্সট প্রদর্শন করা যায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project নির্বাচন করুন।
ধাপ ২: 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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Have a nice day"
android:textColor="@android:color/holo_purple"
android:textSize="36sp"
android:textStyle="bold" />
</RelativeLayout>
ধাপ ৩: অ্যাসেট ফোল্ডারে ফন্ট ফাইল কপি করুন
- Project ভিউ থেকে app → src → main এ রাইট-ক্লিক করে New → Folder → Assets Folder নির্বাচন করুন।
- `assets` ফোল্ডারের ভেতরে আপনার `.ttf` ফাইল (যেমন `fontawesome.ttf` বা `VeganStylePersonalUse-5Y58.ttf`) কপি-পেস্ট করুন।
ধাপ ৪: MainActivity.kt-এ ফন্ট লোড করুন
import android.graphics.Typeface
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val textView: TextView = findViewById(R.id.textView)
// অ্যাসেটস থেকে ফন্ট লোড করুন
val font = Typeface.createFromAsset(assets, "VeganStylePersonalUse-5Y58.ttf")
textView.typeface = font
}
}
ধাপ ৫: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.fontawesome">
<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>
পরীক্ষা করুন
অ্যাপ চালান। TextView-এ কাস্টম ফন্ট প্রয়োগ হয়ে স্টাইলিশ টেক্সট দেখাবে। Font Awesome আইকন ব্যবহার করতে চাইলে আইকনের ইউনিকোড ব্যবহার করুন।
সুবিধা
এই পদ্ধতিতে কোনো থার্ড-পার্টি লাইব্রেরি ছাড়া কাস্টম ফন্ট ব্যবহার করা যায়।