1

According to the Android Typography page Roboto font was introduced in Ice Cream Sandwich. I can download the .ttf files there or I can find it (and many others) in the <android-sdk>/platforms/android-x/data/fonts directory (where x is ICS version and higher).

If I want to actually use this font in my app do I still need to copy this to my assets/fonts directory and setup the font like this or is there some other way of accessing it in my layout XML files?

EDIT:

To clarify the question I really mean any of the Android supplied fonts. So if you look in Android Lollipop's font folder (android-sdk/platforms/android-21/data) there are lots of new fonts.

Assume on a single layout I want to use Roboto-Italic in one TextView and (say) NotoSerif-Bold in another. Can I specify that in my XML layout file using android:typeface="..." or do I need to manually copy the required .ttf files to my font folder and subclass the TextView widget?

Community
  • 1
  • 1
Tim
  • 5,767
  • 9
  • 45
  • 60
  • you can check my answer, with that code you can do that, don't forget check as correct. – Adrian Cid Almaguer Feb 16 '15 at 16:32
  • thanks Adrian... I marked Kevin's answer as correct as he answered before you :(.. you all basically answered my question and I had to choose one... I upvoted everyone though! Really appreciate your help – Tim Feb 16 '15 at 16:44

3 Answers3

2

Hi you would need to set the typeface on a TextView like below:

Typeface yourTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName + ".ttf");
yourTextView.setTypeface(yourTypeface);
Kevin Crain
  • 1,905
  • 1
  • 19
  • 28
  • Thanks for your help. So am I right in thinking that I still have to copy the appropriate `.tff` file to my project's `assets/fonts` directory? – Tim Feb 16 '15 at 13:22
  • yes all your .ttf files you require will go in there :) – Kevin Crain Feb 16 '15 at 15:19
  • ok thanks for that. Its strange that if I am developing for lollipop and above I can't actually specify these fonts (which I know are installed on the phone somewhere!). Instead I have to copy files around and manually create `Typeface` types. – Tim Feb 16 '15 at 15:37
  • Well heres a link to download official Roboto http://www.google.com/design/spec/style/typography.html – Kevin Crain Feb 16 '15 at 15:56
2

The Roboto font is already used by default on android 4.0 and up, so you don't need to do anything.

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
  • Hi.. thanks for your help. I added more detail to my question to clarify it a bit better. Sorry for the ambiguity. – Tim Feb 16 '15 at 13:19
  • In that case, use the answer by @Adrian. You have to copy the TTF file into your project. See [this question](http://stackoverflow.com/questions/12128331/how-to-change-fontfamily-of-textview-in-android) – Jonas Czech Feb 16 '15 at 13:28
2

If you want set Roboto as your font, you must verify what is your version, if you have Android < 4.0 you must do something like this

Typeface font = Typeface.createFromAsset(getAssets(), "fonts/roboto.ttf");

TextView text = (TextView) findViewById(R.id.text);
text.setTypeface(font);

But if your version is 4.0 or higher as JonasCz says, is not necessary, you can read more about this in https://developer.android.com/design/style/typography.html

Note: copy the fonts to assets/fonts. This code is useful also to put any font in your views.

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63