Android में कस्टम फॉन्ट (Font Awesome) का उपयोग कैसे करें
यह उदाहरण दर्शाता है कि Android ऐप में Font Awesome या किसी भी कस्टम .ttf फॉन्ट फ़ाइल का उपयोग करके टेक्स्टव्यू में आइकन कैसे प्रदर्शित करें।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें और प्रोजेक्ट बनाएं।
चरण 2: लेआउट में TextView
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>
चरण 3: Assets फ़ोल्डर में फॉन्ट फ़ाइल जोड़ें
- app मॉड्यूल पर राइट-क्लिक करें → New → Folder → Assets Folder
- Finish पर क्लिक करें
FontAwesome.ttfफ़ाइल डाउनलोड करें (fontawesome.com से) औरsrc/main/assets/फ़ोल्डर में पेस्ट करें
चरण 4: MainActivity.java - कस्टम फॉन्ट लागू करें
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);
// Font Awesome यूनिकोड कैरेक्टर सेट करें
// उदाहरण: "\uf015" = होम आइकन, "\uf007" = यूजर आइकन
textView.setText("\uf015 \uf007 \uf0c0 \uf1e0"); // होम, यूजर, हार्ट, ग्लोब
}
}
चरण 5: 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>
Font Awesome यूनिकोड रेफरेंस
Font Awesome चीट शीट से यूनिकोड मान प्राप्त करें। कुछ सामान्य आइकन:
| आइकन | यूनिकोड | नाम |
|---|---|---|
| | \uf001 | म्यूजिक |
| | \uf007 | यूजर |
| | \uf00d | क्लोज |
| | \uf00c | चेक |
| | \uf011 | पावर ऑफ |
| | \uf015 | होम |
| | \uf058 | कैलेंडर |
| | \uf0e0 | ईमेल |
| | \uf09e | RSS |
| | \uf1eb | WiFi |
बेहतर तरीका: XML में फॉन्ट परिवार (API 26+)
res/font/fontawesome.ttf में फॉन्ट रखें और XML में उपयोग करें:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/fontawesome"
android:text="\uf015" />
कॉमन समस्याएं और समाधान
- आइकन नहीं दिख रहा: फॉन्ट फ़ाइल नाम सही है? (केस-सेंसिटिव)
- बॉक्स/स्क्वायर दिख रहा: फॉन्ट उस कैरेक्टर को सपोर्ट नहीं करता
- साइज छोटा:
android:textSizeबढ़ाएं - रंग बदलने:
android:textColorउपयोग करें
ऐप चलाएं
डिवाइस कनेक्ट करें, Run पर क्लिक करें। TextView में Font Awesome आइकन दिखाई देंगे।