How to use Roboto font in android Project [duplicate]

You can download the Roboto font from here: [Updated 2020-01-28]

You can do it the conventional way by using TypeFace, like this:

Typeface typeface = Typeface.createFromAsset(getAssets(), fontName);

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 TextViews in your application, then you will need to create your own widget that extends TextView.

There is a simple way of doing this. Follow the steps from this answer on SO: (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 use xmlns: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:

    android:text="Custom fonts in XML are easy"
    font:typeface="ROBOTO-REGULAR.TTF" />

Leave a Comment