Adding custom font to Android TextView in Android applications makes Android app more stylish. Also gives your app new look and and works as nice UI enhancement.
In this post I will explain you:
Watch Video for better understanding.
How to add custom font to Android TextView
We are going to create a new Hello World! application and apply the custom font to Hello World! text and see how it works.
- So first start Open Android Studio and start new project with empty activity. After successful building of project
- Go to main_activity.xml and replace the TextView block with below code.
- In above code we added id to TextView for reference in Class file and also made text to center both horizontally and vertically. Increased size of the TextView to 60SP.
- Now run the code in emulator or a real device. After launching app screen should look like below.
- Now open MainActivity.java , inside onCreate method add below lines of Java code (Watch video above to understand the code )
TextView textView = (TextView) findViewById(R.id.textView);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/FontName.ttf");
- In above code first we have created an object for TextView, then created object for font and in the third line applied font to TextView
- In above code FontName.ttf is your font name. Add your font .ttf file inside src/assets/fonts folder and change FontName.ttf to your actual font name.ttf. Font I used here is One Starry Night.ttf. You can use any font you want
- Run the code and see the change. And screen should look like this after launching app text style may vary depending on your Font selection.
This is How to add custom font to Android TextView tutorial hope it helped you. Don’t forget to share the post with your friends. Download the source code from below link.