темы wordpress от лучших производителей шаблонов всего мира. А также сайт mobizoo.ru - обзор мобильных телефонов

How to use Android Roboto font in Honeycomb and earlier versions

A question on how to use Roboto in previous android version comes up time after time.

It’s really simple, actually, and I’m sure everybody knows how to create fonts using Typeface.createFromAssets(), and sure you will be able to write something similar yourself, but, anyway.

Tasks becomes a bit more difficult if you have to support text styles, like bold or italic, because Roboto has different TTF for each style.

So, here’s the small class FontUtils that takes care of all these problems. It can load TTF fonts, cache them and set correct one depending on TextView.textStyle property in layout xml files.

Don’t forget to go to Google Typeography and download latest Roboto TTF files and put them into your /assets/fonts/ directory

* @author Anton Averin
* mailto:

package com.intspirit.util;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FontUtils {

public static interface FontTypes {
public static String LIGHT = "Light";
public static String BOLD = "Bold";

* map of font types to font paths in assets
private static Map fontMap = new HashMap();
static {
fontMap.put(FontTypes.LIGHT, "fonts/Roboto-Light.ttf");
fontMap.put(FontTypes.BOLD, "fonts/Roboto-Bold.ttf");

/* cache for loaded Roboto typefaces*/
private static Map typefaceCache = new HashMap();

* Creates Roboto typeface and puts it into cache
* @param context
* @param fontType
* @return
private static Typeface getRobotoTypeface(Context context, String fontType) {
String fontPath = fontMap.get(fontType);
if (!typefaceCache.containsKey(fontType))
typefaceCache.put(fontType, Typeface.createFromAsset(context.getAssets(), fontPath));
return typefaceCache.get(fontType);

* Gets roboto typeface according to passed typeface style settings.
* Will get Roboto-Bold for Typeface.BOLD etc
* @param context
* @param typefaceStyle
* @return
private static Typeface getRobotoTypeface(Context context, Typeface originalTypeface) {
String robotoFontType = FontTypes.LIGHT; //default Light Roboto font
if (originalTypeface != null) {
int style = originalTypeface.getStyle();
switch (style) {
case Typeface.BOLD:
robotoFontType = FontTypes.BOLD;
return getRobotoTypeface(context, robotoFontType);

* Walks ViewGroups, finds TextViews and applies Typefaces taking styling in consideration
* @param context - to reach assets
* @param view - root view to apply typeface to
public static void setRobotoFont(Context context, View view)
if (view instanceof ViewGroup)
for (int i = 0; i < ((ViewGroup)view).getChildCount(); i++)
setRobotoFont(context, ((ViewGroup)view).getChildAt(i));
else if (view instanceof TextView)
Typeface currentTypeface = ((TextView) view).getTypeface();
((TextView) view).setTypeface(getRobotoTypeface(context, currentTypeface));

As requested - usage example.

Add following in your Activity, where mActivity is your activity instance.

I actually use a global ActivityHelper class in my app, and call this particular string in onPostCreate() method, which is forwarded from each invoked activity.

FontUtils.setRobotoFont(mActivity, (ViewGroup) mActivity.getWindow().getDecorView());


  1. Neil

    Can you also add an example of using this class?

  2. Thanks dude, works like a charm.

  3. Pavel

    How can i use it in my xml layout? I don’t see examples.

    • AntonAverin

      If by ‘use it in xml layout’ you mean changing font type via xml settings – then you should use textStyle property of TextView or other text compontent of your layout.

      Other then that, this particular workaround isn’t supposed to be used in xml layouts.
      The basic idea is to force custom font for a root View of particular Activity, example of that is in the article.

      You can write your own wrapper around View that will also apply a new default font – that wrapper will be usable from xml layout, and you will be able to derrive all your other custom components from that wrapper.
      But I can’t see any practical use of that, because all native layout components extend either View or ViewGroup, so they will not use the font you set in your custom implementation.

  4. Martial

    Work wonderfully, save me a lot of time. Thanks dude.

Leave a Reply