एंड्रॉइड में बटन (या किसी व्यू) पर कस्टम फॉन्ट लागू करना
एंड्रॉइड 8.0 (API 26) से **XML में फॉन्ट रिसोर्स** सपोर्ट करता है — यह सबसे सरल, प्रदर्शन-योग्य तरीका है। उससे पहले या प्रोग्रामेटिक कंट्रोल के लिए Typeface API उपयोग करें। यह ट्यूटोरियल दोनों तरीके दिखाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio → File → New → New Project → "Empty Activity" → Finish।
चरण 2: फॉन्ट फाइल जोड़ें (res/font/)
resपर राइट-क्लिक → New → Android Resource Directory- Resource type: **font** चुनें → OK
res/font/में.ttfया.otfफाइल डालें (उदाहरण:custom_font.ttf)
चरण 3: विधि 1 — XML में फॉन्ट (API 26+, सिफारिश किया गया)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="24dp"
android:gravity="center">
<!-- XML फॉन्ट एट्रिब्यूट (API 26+) -->
<Button
android:id="@+id/btnXmlFont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XML फॉन्ट: कस्टम फॉन्ट"
android:fontFamily="@font/custom_font"
android:textSize="18sp"
android:layout_marginBottom="16dp" />
<!-- प्रोग्रामेटिक फॉन्ट के लिए -->
<Button
android:id="@+id/btnCodeFont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="कोड से फॉन्ट: डिफ़ॉल्ट"
android:textSize="18sp"
android:layout_marginBottom="16dp" />
<!-- सिस्टम फॉन्ट्स (हमेशा उपलब्ध) -->
<Button
android:id="@+id/btnSans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sans (डिफ़ॉल्ट)"
android:fontFamily="sans-serif"
android:layout_marginBottom="8dp" />
<Button
android:id="@+id/btnSerif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Serif"
android:fontFamily="serif"
android:layout_marginBottom="8dp" />
<Button
android:id="@+id/btnMono"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Monospace"
android:fontFamily="monospace"
android:layout_marginBottom="8dp" />
</LinearLayout>
चरण 4: विधि 2 — प्रोग्रामेटिक Typeface (MainActivity.java)
package com.example.customfontdemo;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Button btnCodeFont;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCodeFont = findViewById(R.id.btnCodeFont);
// तरीका 1: रिसोर्स फॉन्ट लोड करें (API 26+)
// Typeface typeface = ResourcesCompat.getFont(this, R.font.custom_font);
// तरीका 2: एसेट्स से फॉन्ट (पुराने API के लिए, या डायनामिक लोडिंग)
// Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/custom_font.ttf");
// तरीका 3: बिल्ट-इन सिस्टम फॉन्ट्स
btnCodeFont.setOnClickListener(v -> {
// टॉगल: Monospace Bold ↔ Serif Bold Italic
if (btnCodeFont.getTypeface() != null &&
btnCodeFont.getTypeface().getStyle() == Typeface.BOLD_ITALIC) {
btnCodeFont.setTypeface(Typeface.MONOSPACE, Typeface.BOLD);
btnCodeFont.setText("कोड से फॉन्ट: Monospace Bold");
} else {
btnCodeFont.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC);
btnCodeFont.setText("कोड से फॉन्ट: Serif Bold Italic");
}
});
// अन्य बटन्स के लिए सिस्टम फॉन्ट्स XML में सेट हैं (fontFamily)
}
}
चरण 5: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.customfontdemo">
<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/Theme.CustomFontDemo">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
चलाएं और टेस्ट करें
- ऐप खोलें — पहला बटन कस्टम फॉन्ट में (अगर
res/font/custom_font.ttfडाला है)। - "कोड से फॉन्ट" बटन दबाएं — Monospace Bold और Serif Bold Italic के बीच टॉगल होगा।
- सिस्टम फॉन्ट बटन्स (Sans, Serif, Monospace) हमेशा काम करते हैं।
🔤 Typeface के मुख्य स्टाइल्स और फॅमिलीज़
| कांस्टेंट | विवरण |
|---|---|
Typeface.NORMAL | नॉर्मल (0) |
Typeface.BOLD | बोल्ड (1) |
Typeface.ITALIC | इटैलिक (2) |
Typeface.BOLD_ITALIC | बोल्ड + इटैलिक (3) |
Typeface.DEFAULT | डिफ़ॉल्ट (Sans) |
Typeface.DEFAULT_BOLD | डिफ़ॉल्ट बोल्ड |
Typeface.SANS_SERIF | सैंस-सेरिफ |
Typeface.SERIF | सेरिफ |
Typeface.MONOSPACE | मोनोस्पेस |
🎨 XML में फॉन्ट वेरिएशन्स (API 26+)
<TextView
android:fontFamily="@font/custom_font"
android:textStyle="bold|italic" <!-- संयुक्त स्टाइल -->
android:textSize="18sp" />
<!-- फॉन्ट वेट (वेरिएबल फॉन्ट्स के लिए API 28+) -->
<TextView
android:fontFamily="@font/variable_font"
android:fontWeight="600" /> <!-- 100-900 -->
🚀 आधुनिक विकल्प: Downloadable Fonts (Google Fonts)
ऐप साइज बढ़ाए बिना Google Fonts उपयोग करें:
// 1. build.gradle
implementation 'androidx.core:core-ktx:1.12.0' // FontsContractCompat के लिए
// 2. res/font/ लिंक फाइल (उदाहरण: lobster.xml)
<font-family xmlns:app="https://schemas.android.com/apk/res-auto"
app:fontProviderAuthority="com.google.android.gms.fonts"
app:fontProviderPackage="com.google.android.gms"
app:fontProviderQuery="Lobster"
app:fontProviderCerts="@array/com_google_android_gms_fonts_certs" />
// 3. उपयोग
android:fontFamily="@font/lobster"
⚡ प्रदर्शन टिप्स
- ✅ **XML fontFamily** — सिस्टम कैश करता है, सबसे तेज़।
- ✅ **Typeface.create()** — एक ही Typeface ऑब्जेक्ट रीयूज़ करें (स्टैटिक फील्ड में स्टोर करें)।
- ❌ **हर क्लिक पर createFromAsset() न करें** — मेमोरी/CPU वेस्ट होता है।
- 🔄 **Downloadable Fonts** — APK साइज बचाता है, ऑन-डिमांड डाउनलोड।
सारांश
- 🎯 **सबसे अच्छा**:
res/font/+ XMLandroid:fontFamily="@font/my_font"(API 26+) - 🔧 **प्रोग्रामेटिक**:
Typeface.create()याResourcesCompat.getFont()— कैश करें! - 🔤 **सिस्टम फॉन्ट्स**:
sans-serif,serif,monospace— हमेशा उपलब्ध। - ☁️ **Google Fonts**: Downloadable Fonts API — कोई बंडलिंग नहीं।
- ✍️
textStyle="bold|italic"XML में स्टाइल जोड़ने के लिए।