Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

एंड्रॉइड में TextView का फॉन्ट फैमिली कैसे बदलें?

एंड्रॉइड में TextView फॉन्ट फैमिली कस्टमाइज़ करना

यह ट्यूटोरियल सिस्टम फॉन्ट्स, कस्टम फॉन्ट्स (XML और प्रोग्रामेटिकली), और Downloadable Fonts API का उपयोग करके TextView का फॉन्ट फैमिली बदलने का तरीका बताता है।

विधि 1: सिस्टम फॉन्ट्स का उपयोग (XML)

एंड्रॉइड कई बिल्ट-इन फॉन्ट फैमिली प्रदान करता है:

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:fontFamily="cursive"
    android:textSize="48sp" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World"
    android:fontFamily="sans-serif-black"
    android:textSize="48sp" />

उपलब्ध सिस्टम फॉन्ट फैमिली:

  • sans-serif (डिफ़ॉल्ट - Roboto)
  • sans-serif-light, sans-serif-thin, sans-serif-medium, sans-serif-black
  • sans-serif-condensed, sans-serif-condensed-light
  • serif, serif-monospace
  • monospace
  • cursive

विधि 2: कस्टम फॉन्ट्स (res/font फ़ोल्डर में) - API 26+

  1. res/font/ फ़ोल्डर बनाएं
  2. .ttf या .otf फॉन्ट फ़ाइलें जोड़ें
  3. XML में उपयोग करें:
<TextView
    android:fontFamily="@font/my_custom_font"
    ... />

विधि 3: प्रोग्रामेटिकली Typeface का उपयोग

import android.graphics.Typeface;

TextView textView = findViewById(R.id.textView);

// एसेट्स फोल्डर से फॉन्ट (fonts फोल्डर बनाएं assets/fonts/ में)
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/custom_font.ttf");
textView.setTypeface(typeface);

// फॉन्ट वेट और स्टाइल के साथ
textView.setTypeface(typeface, Typeface.BOLD_ITALIC);

विधि 4: Downloadable Fonts API (Google Fonts)

ऐप साइज़ बढ़ाए बिना Google Fonts का उपयोग करें:

<!-- res/font/roboto_regular.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="Roboto"
    app:fontProviderCerts="@array/com_google_android_gms_fonts_certs" />

विधि 5: Jetpack Compose में (आधुनिक तरीका)

Text(
    text = "कस्टम फॉन्ट",
    fontFamily = FontFamily(
        Font(R.font.custom_font, FontWeight.Normal),
        Font(R.font.custom_font_bold, FontWeight.Bold)
    )
)

बेस्ट प्रैक्टिसेज

  • फॉन्ट फ़ाइलों के लिए res/font/ का उपयोग करें (API 26+)
  • परफॉरमेंस के लिए फॉन्ट वेरिएंट्स को प्री-लोड करें
  • लाइसेंसिंग प्रतिबंधों की जांच करें
  • वेरिएबल फॉन्ट्स का उपयोग करके फ़ाइल साइज़ कम करें
  1. Android TextView में टेक्स्ट को जस्टिफाई कैसे करें? Android TextView में टेक्स्ट को जस्टिफाई कैसे करें?

    TextView में टेक्स्ट जस्टिफिकेशन Android 8.0 (API 26) से android:justificationMode=inter_word एट्रिब्यूट का उपयोग करके TextView में टेक्स्ट को जस्टिफाई (दोनों किनारों पर संरेखित) किया जा सकता है। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें (न्यूनतम API 26 या अधिक)। चरण 2

  1. Android WebView में फॉन्ट फेस कैसे बदलें? Android WebView में फॉन्ट फेस कैसे बदलें?

    WebView में कस्टम फॉन्ट लोड करने के लिए एसेट्स फोल्डर में HTML/CSS और फॉन्ट फ़ाइल रखने का तरीका। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: लेआउट में WebView res/layout/activity_main.xml: <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=https

  1. Android ListView का रंग और फॉन्ट कैसे बदलें? Android ListView का रंग और फॉन्ट कैसे बदलें?

    Android ListView के रंग और फॉन्ट कस्टमाइज़ करना यह ट्यूटोरियल दिखाता है कि कैसे कस्टम एडाप्टर और लेआउट का उपयोग करके ListView आइटम्स के टेक्स्ट रंग, बैकग्राउंड रंग, और फॉन्ट स्टाइल बदले जाते हैं। चरण 1: प्रोजेक्ट बनाएं Android Studio में नया प्रोजेक्ट बनाएं। चरण 2: मेन लेआउट res/layout/activity_ma