Looking For Anything Specific?

[Tutorial] How To Change The Font Of An Android Application

Hello Everyone,

This is going to be a short tutorial on how to change the font of your android application, we all love to use custom fonts in our application as it makes the application unique. Well, I am not going to bore you with lots and lots of information which you already know, instead, I am going to just explain a quick and simple way to apply a common font to the entire application, or you can just add the fonts and use it whenever necessary. Look the below screenshot which has a custom font set on a TextView you can learn more about the android textview in here,

text font set in android

You Might Have Missed :

How to add custom fonts to your application?

First of all, I would like to give a recommendation, about fonts you may have your own set of fonts collection but they all not going to fit for an application so here is the tip use Google Fonts, these fonts are made for websites so if you're a web developer you might have used them before and you may ask why to use google fonts, Obviously not because they are from google but it has some advantages, it is free and open-source also these fonts have all the styles you need like bold, italic and bold italic and some of the fonts are really looking cool in and android applications interface, so don't wait up and grab yours here Goto: https://fonts.google.com/

You Have Missed :
Once you grab your favorite or newly favored font lets start with add the font into your android application, 

Below is the step by step procedure to add the font to your application:

  • Open android studio 
  • Click on project tab in the sidebar
  • Now right-click on the res folder and navigate to New -> Android Resource Directory
  • Select Resource type as font 
  • Click ok
android set font in style xml

After you click Ok a new directory will be created in your project's res directory, Now copy all the fonts you have downloaded to the font directory.

Note: You must keep the filename with only lower case letter, numbers and underscore( _ ) only, For example, If your font name is OpenSans-BoldItalic.ttf change it to opensans_bolditalic.ttf. Otherwise you will get a compile-time error says :
"'O' is not a valid file-based resource name character: File-based resource names must contain only lowercase a-z, 0-9, or underscore" 

Now that you have successfully added the font to your application you can start by integrating it into your application. There are two days to do that defining the font as the application's default font or you can change the font of components that you really need. Let's start with defining the font to te complete application

How to change the font of the entire application:

Now open your styles.xml which must be under the directory values in the res directory. Add the below line in your AppTheme style tag

<item name="android:fontFamily">@font/yourfont_name</item>

Here yourfont_name is your actual font name and be sure to do the naming conversion to your font file as mentioned above or else you will get an error at compile time. Once added your AppTheme style tag will look like below 

<style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:fontFamily">@font/opensans_regular</item>


Now that you have defined this in the styles.xml file it will be applied to your complete application, for example, if you add a textview, it will automatically set the font family as opensans_regular, And in no, of components if you want to change the font of a single view you can still override the fontFamily by defining an inline attribute to the component.

Quick Tip: If you want to change the text size for the complete application you can also add android:textSize="14sp". It will reflect on the complete application

In case if you don't want to apply the font for all the views instead if you want to change a single textview you can follow below steps

How to change the font of a single android view:

There are no. of android views for which you can apply fonts too, or we can say like that to all the android view which has an android: text attribute you can apply a custom font to it. To apply custom font, an android view simply add the fontFamily attribute to it 


Add this attribute to your view which accepts text and point it to the point which one you would like to use, in my case I have chosen to use opensans_bolditalic. Well in this way you can set fonts to not only textviews also to buttons in android.

Android set font programmatically

 If you want to change the font of textview in run time use the below code to set the font at runtime into a textview, To change the font in runtime by programmatically we need to use the setTypeface method to change font well in kotlin its just typeface  =. 

if(Build.VERSION.SDK_INT >= 26){
                textFive.typeface = resources.getFont(R.font.opensans_bolditalic)
                textFive.typeface = ResourcesCompat.getFont(applicationContext,R.font.opensans_bolditalic)

Also, be sure to add this piece of code inside a button's setOnClickListener in case if you want to know how to set click listener to an android button with kotlin follow below link
And if you want to learn more about textview here is another great tutorial on some advanced and cool things you can do in android's textview