Skip to content

Latest commit

 

History

History
30 lines (20 loc) · 1.27 KB

custom_font.md

File metadata and controls

30 lines (20 loc) · 1.27 KB

Custom Fonts - How to implement FontFamily in Android

We have always needed to add custom fonts in our android apps. But it always involved initializing the font in code and setting it to each widget.

But Android 8.0 (API level 26) introduces a new feature, Fonts in XML, which lets you use fonts as resources. Let's look more into how to implement this.

  • First step: Right-click the res folder and go to New > Android resource directory. The New Resource Directory window appears. In the Resource type list, select font, and then click OK.

  • Second step: Add your font files in the font folder

  • Third step: Add font to TextView in xml:

    <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:fontFamily="@font/montserrat_light" />
    

    Add font to TextView in code:

    Typeface typeface = getResources().getFont(R.font.montserrat_light);
    textView.setTypeface(typeface);