assets ফোল্ডার থেকে কাস্টম ফন্ট লোড করুন
এই টিউটোরিয়ালে দেখানো হয়েছে কিভাবে assets/fonts/ ফোল্ডার থেকে .ttf বা .otf ফন্ট ফাইল লোড করে TextView-এ প্রয়োগ করা যায়।
ধাপ ১: প্রজেক্ট তৈরি
ধাপ ২: ফন্ট ফাইল যোগ করুন
- Project view-এ
app/src/main/assetsফোল্ডার তৈরি করুন (নেই তবে New → Folder → Assets Folder) - সেখানে
fontsফোল্ডার তৈরি করুন - আপনার .ttf ফাইল (যেমন
AlexBrush-Regular.ttf) সেখানে রাখুন
ধাপ ৩: লেআউট
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lets put a smile on your Face!"
android:textSize="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp" />
</RelativeLayout>
ধাপ ৪: MainActivity.java
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tv;
Typeface myFont;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.textView);
myFont = Typeface.createFromAsset(this.getAssets(), "font/AlexBrush-Regular.ttf");
tv.setTypeface(myFont);
}
}
আধুনিক এবং সহজ পদ্ধতি (API 14+ / Android 8.0+)
Android 8.0 (API 26) থেকে **XML-এ ফন্ট রিসোর্স** সাপোর্ট করে। এটি আরও পারফরম্যান্ট এবং সহজ:
১. res/font/ ফোল্ডারে .ttf ফাইল রাখুন
২. XML-এ সরাসরি ব্যবহার করুন
<TextView
android:fontFamily="@font/alexbrush_regular"
android:textSize="50sp" />
৩. প্রোগ্রামেটিক্যালি
Typeface typeface = ResourcesCompat.getFont(this, R.font.alexbrush_regular); textView.setTypeface(typeface);
ফন্ট ফ্যামিলি XML (বোল্ড, ইট্যালিক সাপোর্ট)
<font-family xmlns:android="https://schemas.android.com/apk/res/android">
<font android:fontStyle="normal" android:fontWeight="400" android:font="@font/regular" />
<font android:fontStyle="italic" android:fontWeight="400" android:font="@font/italic" />
<font android:fontStyle="normal" android:fontWeight="700" android:font="@font/bold" />
</font-family>
সেরা অনুশীলন
- API 26+ এর জন্য
res/font/ব্যবহার করুন (অ্যাপ বান্ডেলে অপ্টিমাইজড থাকে) - পুরানো API-র জন্য
Calligraphyলাইব্রেরি বা assets পদ্ধতি ব্যবহার করুন - ফন্ট লাইসেন্স চেক করুন (কমার্শিয়াল ব্যবহারে)