android custom fonts

‮ww‬w.theitroad.com

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:

  1. Add the font file to your project's assets folder. The font file should be in either TrueType (.ttf) or OpenType (.otf) format.

  2. In your app's res folder, create a new folder named font.

  3. 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']
        }
    }
}
  1. 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.

  1. 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.