How to use a custom font for a TextView? [SOLVED] – Android Studio


First of all make sure that you have an assets folder in your project with this location. Then create a folder named ‘Fonts’ inside it and paste the font file there. So that the font file has a location like this: app/src/main/assets/fonts/font.ttf

You can download the font files which are in .ttf format from Google Fonts or any other website.

Then add the following code to the activity.
First , get the reference of the text view in the code. Its syntax is given below −

TextView tx = (TextView)findViewById(R.id.textview1);

The next thing you need to do is to call static method of Typeface class createFromAsset() to get your custom font from assets. Its syntax is given below −

Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf");

The last thing you need to do is to set this custom font object to your TextView Typeface property. You need to call setTypeface() method to do that. Its syntax is given below −

tx.setTypeface(custom_font);

So your complete code looks like this:

TextView tx = (TextView)findViewById(R.id.textview1);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf");
tx.setTypeface(custom_font);

Reference:
https://www.tutorialspoint.com/android/android_custom_fonts.htm

PhD researcher at Friedrich-Schiller University Jena, Germany. I'm a physicist specializing in theoretical, computational and experimental condensed matter physics. I like to develop Physics related apps and softwares from time to time. Can code in most of the popular languages. Like to share my knowledge in Physics and applications using this Blog and a YouTube channel.



One thought on “How to use a custom font for a TextView? [SOLVED] – Android Studio

  1. hello your code work in android studio. this code is only one font so i have many more font.
    i have create one button and change to font many type. how can do this please help me. thanks for code.

Leave a Reply

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