Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we have to copy Font TTF everytime we want to change font in application

Tags:

android

Previously, to make my app workable in Gingerbread device and above, I have to copy the Robotto font resource into asset folder. This is because Gingerbread doesn't come with Robotto font itself.

enter image description here

However, let say, I decide to deploy my app to Jelly Bean device only.

  1. Do I still need to copy font resources into my asset folder manually? Can I use font resources from system itself? Is it something encourage-able? I was thinking, without supplying my own font files, I can make my app smaller.
  2. This is the code to get TypeFace from asset folder.

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

    If I want to get TypeFace directly from system itself, how?

like image 322
Cheok Yan Cheng Avatar asked Dec 15 '22 15:12

Cheok Yan Cheng


1 Answers

The good news is, if you're supporting 4.1, it's dead simple. Check out this link and scroll down to fonts for the full details, but basically, you'll have three font families (Roboto, Roboto Light, RobotoCondensed) to choose from, and four styles for each (normal, bold, italic, bold italic).

In XML, you can just use the standard text attributes:

android:fontFamily="sans-serif"
android:fontFamily="sans-serif-light"
android:fontFamily="sans-serif-condensed"

android:textStyle="bold"
android:textStyle="italic"
android:textStyle="bold|italic"

Or programatically you can acquire them like so:

Typeface robotoLightItalic = Typeface.create("sans-serif-light", Typeface.ITALIC);
like image 121
Kevin Coppock Avatar answered Feb 02 '23 00:02

Kevin Coppock