Hello world, today we are going to see how we can set the different font
families to the textview.
Why we need to use a different font for textview because simple or default font family of textview looks so simple and to give a professional look to our app we need custom fonts for textview. Let’s see how to add custom fonts in the android application.
Below is the example of the custom fonts added to the textview.
In the above image first font is default one and the rest are custom added
To add the fonts first we need to download the .ttf files.
Download .ttf fonts for textview
Download one or more fonts that you like to add in the app.
Create a font folder and add fonts
After downloading we need to add these fonts in the font folder in the android
To make the font folder right-click on res folder then click on new then click
on Android Resource Directory and after that choose resource type to font then
click on the OK button.
After this, your font folder will be created then move your downloaded fonts
in the font folder in the android project.
Remember only add .ttf files in the font folder.
Now we have added the font now let’s see how we can use them.
Set Font to TextView in XML
To set the font to textview just use this
For example, we have downloaded a font name montserrat.ttf
then our textview will look like this.
This the XML way to set font family.
Set Font to TextView Programmingly
To set the font in the java file then you can do it like this. Let’s say we
have an alata.ttf font in the font folder.
Sometimes you may want to make a custom textview that has your custom font as
the default font.
In that case, you can make a new java file and extend to the
AppCompactTextView and set your font. See the below code for better understanding.
To set the font we made a init function in that function we use the
setTypeface function and pass our typeface font. In this case, our font name
Now you can use your custom textview in the XML and treat it like the normal textview but with the custom font family. You can use this in your whole app.
If you learned something new then do share this article with your friends
and batchmates and don’t forget to subscribe to our newsletter if you
haven’t joined already for the latest updates.
Thanks for reading have a nice day.