Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Roboto font for earlier devices

Tags:

I would like to use the Roboto font in my Android application and make sure it works for earlier versions of Android that don't have the font installed. I know I can do this by using Typeface.createFromAsset() and then manually setting the font for each of my TextViews/Buttons/Other-Objects. It seems like a big pain to do this for every object I show on the screen though.

My question is, is there a better way to do this? Some helper class or a way to set a custom font in a .xml theme file? Anything automated would be better than manually listing out every object on each screen and changing the font.

Thanks!

like image 823
Blather Avatar asked Mar 21 '12 02:03

Blather


People also ask

What is Roboto font good for?

Roboto is a neo-grotesque sans-serif typeface family developed by Google as the system font for its mobile operating system, Android, and released in 2011 for Android 4.0 "Ice Cream Sandwich".

Can I use Roboto font commercially?

Yes, you can use them commercially, and even include them within a product that is sold commercially.

Why is Roboto so popular?

Roboto Serif is designed to be easier on the eyes and provides an ideal reading experience across various screen sizes and in print. Google and Commercial Type have introduced a new variable typeface called Roboto Serif.


2 Answers

Above accepted answer is correct but I just wanted to supply my implementation here

My utility class:

package com.example.utils;

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

public class AndroidUtils
{
    private static Typeface robotoTypeFace;

    public static void setRobotoFont (Context context, View view)
    {
        if (robotoTypeFace == null)
        {
            robotoTypeFace = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto/Roboto-Regular.ttf");
        }
        setFont(view, robotoTypeFace);
    }

    private static void setFont (View view, Typeface robotoTypeFace)
    {
        if (view instanceof ViewGroup)
        {
            for (int i = 0; i < ((ViewGroup)view).getChildCount(); i++)
            {
                setFont(((ViewGroup)view).getChildAt(i), robotoTypeFace);
            }
        }
        else if (view instanceof TextView)
        {
            ((TextView) view).setTypeface(robotoTypeFace);
        }
    }
}

How to use it, assuming this is an Activity:

AndroidUtils.setRobotoFont(this, view);

To set the same font to all the TextView you can use the decorView of your activity:

ViewGroup godfatherView = (ViewGroup)this.getWindow().getDecorView();
AndroidUtils.setRobotoFont(this, godfatherView);

If you have adapters or fragments, don't forget to set their font as well.

See here also.

like image 153
Arnaud Avatar answered Oct 10 '22 03:10

Arnaud


Retrieve all views inside activity, check its type and apply appropriate action.

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Regular.ttf");
for (View view : allViews)
{
 if (view instanceof TextView) 
 {
    TextView textView = (TextView) view;
    textView.setTypeface(typeface);
  }
}
like image 37
Pawan Maheshwari Avatar answered Oct 10 '22 04:10

Pawan Maheshwari