This question already has an answer here:
I have developed one android project. In this project the text font defaults to android:sans.
Now I want to replace the default text font to roboto font for my entire project.
How can I do this?
This question already has an answer here:
I have developed one android project. In this project the text font defaults to android:sans.
Now I want to replace the default text font to roboto font for my entire project.
How can I do this?
Download and unzip the
Roboto
font zip fileCreate a
assets
folder in your project if you don't have one already. Am assuming you are using Android Studio, this is how to do it.Create a new directory in the assets folder, name it
font
.Open your unzipped file and copy the font style of your choice. Your font folder shouls look like this:
You can now use this font anywhere in your app like this:
You can download the Roboto font from here: https://www.google.com/design/spec/resources/roboto-noto-fonts.html.
You can do it the conventionaly way by using
TypeFace
, like this:Note: The above will have to be done in every
Activity
.Alternatively, if, for example, you want to apply the Roboto font to all the
TextView's
in your application, then you will need to create your own widget that extendsTextView
.There is a simple way of doing this. Follow the steps from this answer on SO: https://stackoverflow.com/a/9199258/450534 (full props to leocadiotine for the solution. I have used it before and it works like a charm)
EDIT: Think of
your_namespace
as a marker for you to give it a name of your choice. For example, when integrating Admob in XML, I usexmlns:ads
. You can use, for example:xmlns:font
or something descriptive.As for what the custom.ttf stands for, it is basically the font file with its extension that you need to copy in your
Assets
folder. For example, if you are using ROBOTO-REGULAR.TTF, then replace the custom.ttf with ROBOTO-REGULAR.TTF. Using this example, the entire code should look this this:One more remark for the usage of
Typeface.createFromAsset()
function. When I had many calls on it it significantly impacted the inflate time. To overcome this issue we created a singleton instance of theTypeface
like thisYou can do this easily by using the Typerlib.
Add this to your
build.gradle
Then you can use the font by
It includes all the current fonts in Roboto ,Like:
To view all available fonts use Android Studio Auto complete after the class Font by pressing Ctrl + space.