How to set custom font in XML file?

44 sec read

In this post, We detail how to set custom font in Android O ( API level 26) and best way of do it.

  • Right click the res folder and go to New -> Android resource directory. The New Resource Directory window appears and select Font in Resource Type.

  • Add font files in the Font folder.The folder structure below generates

  • if we preview the fonts, double-click a font file. Next, we need to create a font family

Creating a font family

  • Right click the Font folder and go to New -> Font resource file. The New Resource File window appears and enter the File name.


Each font file, style, and weight attribute in the <font> element. The following XML illustrates adding font-related attributes in the font resource XML

Add Font in Xml file

Adding Fonts Programatically
getFont(int) method is used to retrieve the font programatically and provide the resource identifier of the font to retrieve and returns typeface object.
[source language=java]
Typeface typeface = ResourcesCompat.getFont(MainActivity.this,R.font.proxima_nova);
textView.setTypeface(typeface);
[/source]

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *