Android set Roboto font with bold, italic, regular,... (something like custom font family)

zmeda picture zmeda · Mar 2, 2012 · Viewed 13.8k times · Source

I know ho to set custom font programmatically inside Android app. Is there any way to load typeface for custom font (assets) and Android framework will use proper file based on bold, italic and so on?

For example now I'm trying to set Roboto font to some TextView

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Regular.ttf");
textView.setTypeface(typeface);

It works ok. But since I set TextView inside xml layout to bold , text is not bolded

<TextView
    android:id="@+id/my_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="50dp"
    android:textStyle="bold"
    android:gravity="center"
    android:text="@string/my_text"
    android:textColor="@color/my_foreground"
    android:textSize="24dp" />

How to load typeface from assets properly that this will work?

textView.setTypeface(typeface, Typeface.BOLD);

Inside my assets dir there is only one "font family"

Roboto-Black.ttf
Roboto-BlackItalic.ttf
Roboto-Bold.ttf
Roboto-BoldCondensed.ttf
Roboto-BoldCondensedItalic.ttf
Roboto-BoldItalic.ttf
Roboto-Condensed.ttf
Roboto-CondensedItalic.ttf
Roboto-Italic.ttf
Roboto-Light.ttf
Roboto-LightItalic.ttf
Roboto-Medium.ttf
Roboto-MediumItalic.ttf
Roboto-Regular.ttf
Roboto-Thin.ttf
Roboto-ThinItalic.ttf

How to load all that fonts inside one typeface/family?

Answer

AAverin picture AAverin · Sep 12, 2012

Not sure if it's a good idea to post a link, but anyway... Here is my blog post on this topic, it has a class that solves this problem fully. http://anton.averin.pro/2012/09/12/how-to-use-android-roboto-font-in-honeycomb-and-earlier-versions/