android custom fonts
You can use custom fonts in your Android app to give it a unique look and feel. Here are the steps to use custom fonts in your Android app:
Add the font file to your project's assets folder. The font file should be in either TrueType (.ttf) or OpenType (.otf) format.
In your app's
res
folder, create a new folder namedfont
.Add the following code to your app's
build.gradle
file to specify the font folder as a resource directory:
android { ... sourceSets { main { res.srcDirs += ['src/main/res/font'] } } }
- In your layout XML file, specify the font family for the TextView, EditText or Button you want to use custom font:
<TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, World!" android:fontFamily="@font/my_custom_font" />
In this example, the android:fontFamily
attribute references the font file named "my_custom_font.ttf" that you added to the font
directory.
- If you want to set the font programmatically, use the following code:
Typeface typeface = ResourcesCompat.getFont(this, R.font.my_custom_font); textView.setTypeface(typeface);
This code retrieves the font file from the resources and sets it as the typeface for the TextView.
Note that custom fonts can increase the size of your app, so be mindful of the size of the font files you use. Also, make sure you have the proper licensing rights for any custom fonts you use in your app.