এই উদাহরণটি দেখায় কিভাবে নেটিভ অ্যান্ড্রয়েড অ্যাপ্লিকেশনে Font Awesome ব্যবহার করা যায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project এ যান এবং প্রজেক্ট তৈরি করার জন্য সকল প্রয়োজনীয় তথ্য পূরণ করুন।
ধাপ ২: লেআউট XML ফাইল যোগ করুন
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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="FontAwesome"
android:textSize="48sp"
android:textStyle="bold" />
</RelativeLayout>
ধাপ ৩: অ্যাসেট ফোল্ডারে ফন্ট ফাইল রাখুন
একটি নতুন ডাউনলোড অ্যাসেট ফোল্ডার তৈরি করুন। fontAwesome.ttf ফাইলটি অ্যাসেট ফোল্ডারে কপি-পেস্ট করুন।
ধাপ ৪: MainActivity.java ফাইলে কোড যোগ করুন
src/MainActivity.java ফাইলে নিচের কোডটি যোগ করুন। Typeface.createFromAsset() ব্যবহার করে কাস্টম ফন্ট লোড করা হয়েছে।
package app.com.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textView);
Typeface font = Typeface.createFromAsset(getAssets(), "FontAwesome.ttf");
textView.setTypeface(font);
}
}
ধাপ ৫: AndroidManifest.xml ফাইল
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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-তে আপনার প্রজেক্টের যেকোনো এক্টিভিটি ফাইল খুলুন এবং টুলবার থেকে Run আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস সিলেক্ট করুন এবং ডিভাইসে ডিফল্ট স্ক্রিন দেখুন।