apw2012
apw2012

Reputation: 233

Change TextView Font?

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"  />

Upvotes: 2

Views: 12967

Answers (3)

ImI
ImI

Reputation: 208

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);

Upvotes: 0

Siddharth Lele
Siddharth Lele

Reputation: 27748

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/

Upvotes: 6

Ted Hopp
Ted Hopp

Reputation: 234795

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.

Upvotes: 6

Related Questions