2

How can I change font type in Android TextView?

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Welcome!"
    android:textSize="20sp"  />
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
apw2012
  • 233
  • 3
  • 5
  • 12

3 Answers3

6

You can use the android:typeface attribute. For example:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Welcome!"
    android:typeface="sans"
    android:textSize="20sp"  />

The XML attribute only allows the values normal, sans, serif, and monospace. If you want to use a different Typeface (perhaps a custom one that you ship with your app), you will have to do it in code by calling setTypeface() for the view after the TextView is loaded.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Now what are some of the different type faces? – apw2012 Sep 23 '12 at 16:05
  • @apw2012 - You can usually only depend on a single set of type faces being present in the system--the ones accessed through the allowed XML values. It used to be "Droid Sans", "Droid Sans Mono" and "Droid Serif". As of Ice Cream Sandwich (4.0), it's the "Roboto" family (described [here](http://developer.android.com/design/style/typography.html)). For anything else, you'll need to ship it with your app and load it using one of the `TypeFace.createFrom...` methods (such as @Siddharth describes). – Ted Hopp Sep 23 '12 at 16:11
6

To add to Ted Hopp's answer, if you are looking at a custom font for your TextView, in your Activity from where you reference the TextView, use this code example:

Typeface blockFonts = Typeface.createFromAsset(getAssets(),"ROBOTO-MEDIUM_0.TTF");
TextView txtSampleTxt = (TextView) findViewById(R.id.your_textview_id);
txtSampleTxt.setTypeface(blockFonts);

Although, before you can use this, you will need to copy the font/s of your choice in the assets folder.

You can also look at one of my answer on SO which has a couple of websites you can use to download fonts. They are:

http://www.google.com/webfonts

http://www.fontsquirrel.com/

Community
  • 1
  • 1
Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
  • 1
    To clarify, you only need to create a `fonts` sub-folder if you want to use a path that starts with `"fonts/"` as the asset name as you are doing. Fonts can also be loaded from the root assets folder. – Ted Hopp Sep 23 '12 at 16:12
  • Typeface blockFonts = Typeface.createFromAsset(getAssets(),"fonts/Lobster.TTF"); TextView txtSampleTxt = (TextView) findViewById(R.id.welcome); txtSampleTxt.setTypeface(blockFonts); – apw2012 Sep 23 '12 at 16:19
  • Okay so the thing is crashing when it opens. I put the code provided and changed it to the needed font under the oncreate/setlayout in the java – apw2012 Sep 23 '12 at 20:33
  • @apw2012: I will typically drop the font in the folder, then click on it, press F2 for the rename dialog, select the entire font name with the extension and then paste it in my java code. Please look at my edit after being corrected by _Ted_ – Siddharth Lele Sep 24 '12 at 03:26
  • I'm on android, not a computer. I've been trying to use AIDE. – apw2012 Sep 24 '12 at 20:17
  • @apw2012: Ohhh..... well, then just make sure you type it out to the T. Including the case in the extension too. – Siddharth Lele Sep 24 '12 at 20:51
0

You can programatically set the desire font by placing your font file (example.tff) style in Assets/fonts and then give your own string into fontpath variable

    String fontPath="fonts/Unkempt-Regular.ttf";
    TextView aboutus=(TextView)findViewById(R.id.aboutus);
    Typeface type=Typeface.createFromAsset(getAssets(), fontPath);
    aboutus.setTypeface(type);
ImI
  • 198
  • 1
  • 8