You can download the Roboto font from here:
https://fonts.google.com/specimen/Roboto. [Updated 2020-01-28]
You can do it the conventional way by using TypeFace
, like this:
Typeface typeface = Typeface.createFromAsset(getAssets(), fontName);
textView.setTypeface(typeface);
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 extends TextView
.
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 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:
<your.package.widget.TypefacedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:font="http://schemas.android.com/apk/res/your.package"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Custom fonts in XML are easy"
android:textColor="#FFF"
android:textSize="14dip"
font:typeface="ROBOTO-REGULAR.TTF" />