यह उदाहरण दर्शाता है कि Kotlin में कस्टम TTF फ़ॉन्ट फ़ाइल का उपयोग करके Android ऐप में Font Awesome आइकन कैसे उपयोग करें।
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio: File → New Project।
चरण 2 - फ़ॉन्ट फ़ाइल जोड़ें
- assets फोल्डर बनाएं:
src/main/assets(यदि मौजूद नहीं है) - Font Awesome की
.ttfफ़ाइल (उदाहरण:fontawesome-webfont.ttf) को assets फोल्डर में कॉपी करें
चरण 3 - 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/textViewIcon"
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>
चरण 4 - 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 = findViewById<TextView>(R.id.textViewIcon)
// Assets फोल्डर से फ़ॉन्ट लोड करें
val typeface = Typeface.createFromAsset(assets, "fontawesome-webfont.ttf")
textView.typeface = typeface
// Font Awesome यूनिकोड कैरेक्टर सेट करें (उदाहरण: \uf004 = दिल आइकन)
textView.text = "\uF004 \uF015 \uF0A1" // दिल, होम, लाइटबल्ब
}
}
चरण 5 - AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.fontawesome">
<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>
ऐप चलाएं
ऐप चलाएं। TextView में Font Awesome आइकन (दिल, होम, लाइटबल्ब) टेक्स्ट के रूप में प्रदर्शित होंगे।
सुझाव: Font Awesome के नवीनतम संस्करण के लिए fontawesome.com से फ्री वेबफॉन्ट डाउनलोड करें। यूनिकोड मान चीटशीट से प्राप्त करें।
आधुनिक विकल्प: Android 8.0+ (API 26+) में XML में फ़ॉन्ट फीचर का उपयोग करें:res/font/में TTF रखें औरandroid:fontFamily="@font/fontawesome"का उपयोग करें।