यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android में बटन टेक्स्ट के लिए कस्टम फॉन्ट (Typeface) कैसे सेट किया जाता है। बिल्ट-इन फॉन्ट्स (MONOSPACE, SERIF, SANS_SERIF) और कस्टम TTF/OTF फॉन्ट्स दोनों का उपयोग किया जा सकता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: 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: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" />
<Button
android:id="@+id/btnChangeFont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Click to change font of the Button" />
<Button
android:id="@+id/btnChangeFont2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnChangeFont"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:text="Click to change font of the Button" />
</RelativeLayout>
चरण 3: MainActivity.kt में फॉन्ट सेट करें
import android.graphics.Typeface
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var btnChangeFont: Button
lateinit var btnChangeFont2: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
btnChangeFont = findViewById(R.id.btnChangeFont)
btnChangeFont.setOnClickListener { btnChangeFont.setTypeface(Typeface.MONOSPACE, Typeface.BOLD) }
btnChangeFont2 = findViewById(R.id.btnChangeFont2)
btnChangeFont2.setOnClickListener { btnChangeFont2.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC) }
}
}
चरण 4: कस्टम फॉन्ट फ़ाइल उपयोग करना (assets/fonts/)
assets/fonts/custom_font.ttf में फॉन्ट फ़ाइल रखें:
val customTypeface = ResourcesCompat.getFont(this, R.font.custom_font) // या API 26+ के लिए: val customTypeface = Typeface.createFromAsset(assets, "fonts/custom_font.ttf") button.typeface = customTypeface
चरण 5: XML में फॉन्ट सेट करना (Android 8.0+)
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/custom_font"
android:textStyle="bold" />
चरण 6: AndroidManifest.xml कॉन्फ़िगर करें
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.q11">
<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 डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में किसी एक्टिविटी फ़ाइल को खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप चलने पर डिफ़ॉल्ट स्क्रीन देखें।
उपलब्ध बिल्ट-इन Typeface स्टाइल्स:
Typeface.NORMAL- सामान्यTypeface.BOLD- बोल्डTypeface.ITALIC- इटैलिकTypeface.BOLD_ITALIC- बोल्ड इटैलिकTypeface.MONOSPACE- मोनोस्पेसTypeface.SERIF- सेरिफTypeface.SANS_SERIF- सैंस-सेरिफ