Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 324
zmeda Avatar asked Mar 02 '12 10:03

zmeda


People also ask

What is the default font family in Android?

"Roboto and Noto are the standard typefaces on Android and Chrome." From Wiki, "Roboto is a sans-serif typeface family developed by Google as the system font for its mobile operating system Android."

Can I modify Roboto font?

No, the font properties cannot be changed. There are currently no plans to support custom fonts or sizes. MAX Agent is implemented using the Roboto font which was selected specifically for being readable at small font sizes.


1 Answers

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/

like image 61
AAverin Avatar answered Sep 25 '22 13:09

AAverin