यह उदाहरण दर्शाता है कि Android ऐप में एसेट्स फोल्डर से कस्टम फॉन्ट (TTF फाइल) को TextView पर कैसे लागू किया जाए।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में नया प्रोजेक्ट बनाएं।
चरण 2: फॉन्ट फाइल जोड़ें
assets/font/ फोल्डर बनाएं और उसमें AlexBrush-Regular.ttf (या कोई भी .ttf फाइल) रखें।
चरण 3: लेआउट XML
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lets put a smile on your Face!"
android:textSize="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp" />
</RelativeLayout>
चरण 4: MainActivity.java में Typeface बनाएं
src/MainActivity.java:
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tv;
Typeface myFont;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = findViewById(R.id.textView);
myFont = Typeface.createFromAsset(this.getAssets(), "font/AlexBrush-Regular.ttf");
tv.setTypeface(myFont);
}
}
नोट (API 26+) : Android 8.0 (API 26) और उससे ऊपर के लिए, आप
res/font/फोल्डर में फॉन्ट रखकर XML मेंandroid:fontFamily="@font/your_font"का उपयोग कर सकते हैं। यह अधिक कुशल तरीका है।
चरण 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>
ऐप चलाएं
टेक्स्टव्यू में कस्टम फॉन्ट लागू हुआ दिखेगा।

