57

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?

lenik
  • 23,228
  • 4
  • 34
  • 43
Jeeva
  • 641
  • 1
  • 6
  • 7

5 Answers5

65

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 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: 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" />
big_m
  • 1,408
  • 1
  • 12
  • 24
Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
  • 5
    Font can be found in SDK in `platforms/android-X/data/fonts/` since X 11 (if I remember correctly when it was introduced). – Marcin Orlowski Nov 24 '12 at 09:19
  • @WebnetMobile.com: I know. But I think there are a few missing in the SDK. The one off the Android webpage has 16. – Siddharth Lele Nov 24 '12 at 09:24
  • Sidddharth Lele: u mentioned in xml file that your_namespace, what it is? , and my assest folder not having custom.tff , then what to do, and what is the use of custom.ttf ?? – Jeeva Nov 26 '12 at 12:28
  • @SiddharthLele how can I use bold style under this configuration with Trykker font?. I'm trying android:textStyle="bold" but does not take effect – Ricardo Mar 29 '14 at 12:20
  • 2
    @Ricardo: When you use a Font other than he default Android Fonts, the attribute `android:textStyle="...."` is ignored. To make it bold, you will need to use the bold version .ttf / .otf font file. The _Trykker_ font, however, has just the regular version and no other variants. – Siddharth Lele Mar 29 '14 at 12:40
  • is it just me or is there no download link on the page you linked to download roboto font? – Adam Johns Jul 10 '15 at 02:33
  • @adamjohns: It's not just you. They changed the link when they rolled out Material Design pages. And I didn't realize the link doesn't work until you commented. Thank you. Link has been updated. – Siddharth Lele Jul 10 '15 at 02:39
  • @IceMAN To do this for each and every widget in an application is an insane amount of work, And even worse, totally inflexible. – f470071 Oct 27 '15 at 09:34
  • @f470071: I agree. Entirely. But that being said, there simply was no easier way of doing that when this answer was posted. Even libraries like [Calligraphy](https://github.com/chrisjenx/Calligraphy), which is a better alternative to this answer, still needs every widget to define a custom font. Or use just one custom throughout the application. At this point, my best guess is, unless Google provides an _out-of-the-box_ option, this is as best as it goes. – Siddharth Lele Oct 27 '15 at 13:23
  • 4
    if you are setting midSdk to 16, you can set the roboto font natively, no need to download any font. Here is the example: From android 4.1 / 4.2, the following Roboto font families are available: android:fontFamily="sans-serif" // roboto regular android:fontFamily="sans-serif-light" // roboto light android:fontFamily="sans-serif-condensed" // roboto condensed android:fontFamily="sans-serif-thin" // roboto thin (android 4.2) android:fontFamily="sans-serif-medium" – shaby Mar 18 '16 at 09:48
25
  1. Download and unzip the Roboto font zip file

  2. Create 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. enter image description here

  3. Create a new directory in the assets folder, name it font.

  4. Open your unzipped file and copy the font style of your choice. Your font folder shouls look like this:

enter image description here

You can now use this font anywhere in your app like this:

   Typeface roboto = Typeface.createFromAsset(context.getAssets(), 
  "font/Roboto-Bold.ttf"); //use this.getAssets if you are calling from an Activity
   txtView.setTypeface(roboto);
Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
6
txtView = (TextView) findViewById(R.id.txtView);

Typeface myTypeface = Typeface.createFromAsset(
                          this.getAssets(),
                          "font/Robot.otf");

txtView.setTypeface(myTypeface);
David Segonds
  • 83,345
  • 10
  • 45
  • 66
Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45
6

You can do this easily by using the Typerlib.

This library wraps the complexity of adding a font resource in your project and recycle those recently created font inorder to maximize your app's perfromance.

Add this to your build.gradle

dependencies {
    compile 'com.elmargomez.typer:typerlib:1.0.0'
}

Then you can use the font by

TextView txtView1 = (TextView) findViewById(R.id.yourTxtView1);
TextView txtView2 = (TextView) findViewById(R.id.yourTxtView2);
TextView txtView3 = (TextView) findViewById(R.id.yourTxtView3);
TextView txtView4 = (TextView) findViewById(R.id.yourTxtView4);

txtView1.setTypeface(Typer.set(yourContext).getFont(Font.ROBOTO_REGULAR));
txtView2.setTypeface(Typer.set(yourContext).getFont(Font.ROBOTO_CONDENSED_ITALIC));
txtView3.setTypeface(Typer.set(yourContext).getFont(Font.ROBOTO_THIN));
txtView4.setTypeface(Typer.set(yourContext).getFont(Font.ROBOTO_BOLD));

It includes all the current fonts in Roboto ,Like:

    Font.ROBOTO_MEDIUM
    Font.ROBOTO_REGULAR
    etc.

To view all available fonts use Android Studio Auto complete after the class Font by pressing Ctrl + space.

Enzokie
  • 7,365
  • 6
  • 33
  • 39
3

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 the Typeface like this

public static Typeface getTypeFace() {
        if (fromAsset == null) {
            fromAsset = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Medium.ttf");
        }
        return fromAsset;
    }
Vishwajit Palankar
  • 3,033
  • 3
  • 28
  • 48
Szabolcs Becze
  • 507
  • 1
  • 5
  • 10