39

I have already read some articles and searched on Google, but I failed to do it.

My problem is regarding the font-face.

In Android, there are only 4 attributes in "android:typeface": Normal, Sans, Serif, Monospace.

So what do I have to do to use "Verdana" in my application?

Please suggest me a correct way to use this font in my Android application.

Adinia
  • 3,722
  • 5
  • 40
  • 58
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • Check this post: [http://stackoverflow.com/questions/2888508/how-to-change-the-font-on-the-text-view-in-android](http://stackoverflow.com/questions/2888508/how-to-change-the-font-on-the-text-view-in-android) – Praveen Jul 08 '10 at 12:50
  • Check this one also : http://stackoverflow.com/a/14558090/693752 – Snicolas Jan 28 '13 at 08:25
  • http://stackoverflow.com/questions/9030204/how-to-use-custom-font-in-android-xml/9035924#9035924 – Vins May 02 '13 at 13:31
  • 1
    @ASP it would not be needed anymore since Google has already provided an option with Android O to set custom fonts :) Check this out https://medium.com/@pareshmayani/android-o-whats-new-for-androiddev-7906222e569b#.n8kbubcg2 – Paresh Mayani Mar 27 '17 at 07:45

7 Answers7

77

This is a simple example... create a folder in the root of your project called assets/fonts/ then paste the TTF font file (in this case Verdana.ttf). Then, if you want to apply that font to, say a TextView, do the following:

import android.graphics.Typeface;

public class FontSampler extends Activity {
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    TextView tv=(TextView)findViewById(R.id.custom);
    Typeface face=Typeface.createFromAsset(getAssets(),
                                          "fonts/Verdana.ttf");

    tv.setTypeface(face);
  }
}

This example was taken from the ComonsWare book (written by Mark Murphy). You can download the full example from GitHub.

iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
Cristian
  • 198,401
  • 62
  • 356
  • 264
4

You can use PixlUI at https://github.com/neopixl/PixlUI

import their .jar and use it in XML

 <com.neopixl.pixlui.components.textview.TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    pixlui:typeface="GearedSlab.ttf" />
guest2343sdfdfs
  • 111
  • 1
  • 1
  • I tried your library, but it seems there is no effect for android:textStyle="bold" if I change TextView to pixlui TextView. (v 1.0.5) – mrmoment Oct 10 '14 at 03:48
3

Well!!
This question is pretty old but still if someone is looking for the answer(in 2015) on how to apply custom font to all the Textviews through xml code directly see below:

First:
we need to add custom font inside assets folder inside your app directory:
.ttf or .otf both work in case of Android

Second:
Create Class CustomTextView which extends TextView like below:

public class CustomTextView extends TextView {

public CustomTextView(Context context) {
    super(context);
   }

public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr)   {
    super(context, attrs, defStyleAttr);
   }

public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
   }

@Override
public void setTypeface(Typeface tf) {
    super.setTypeface(FontCache.getFont(getContext(),"fonts/<font_name>"));
   }
 }

Third:
FontCache class being used inside CustomTextView's setTypeface() method.Purpose is to do basic Font Caching using HashMap:

public class FontCache {

private static Map<String,Typeface> fontMap = new HashMap<String,Typeface>();

public static Typeface getFont(Context context,String fontname){
    if(fontMap.containsKey(fontname)){
        return fontMap.get(fontname);
       }
    else{
        Typeface tf = Typeface.createFromAsset(context.getAssets(),fontname);
        fontMap.put(fontname,tf);
        return tf;
      }
    }
}

Fourth:[Final step] All we do now is use the CustomTextView directly inside our xml file wherever custom font textview is required:

<<package_name>.CustomTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Custom Font Text"
    android:textSize ="18sp"
    android:textAppearance="?android:textAppearanceSmall"
    android:id="@+id/custom_txt"
   />

Sorry, if this has already been posted somewhere on SO. Just thought to share if it helps someone!!

PunitD
  • 2,293
  • 1
  • 20
  • 29
2

You can use simple EasyFonts third party library to set variety of custom font to your TextView. By using this library you should not have to worry about downloading and adding fonts into the assets/fonts folder. Also about Typeface object creation.

This library does not provides Verdana Font face.

But provide following font faces. Which might you would like to use.

  • Roboto
  • Droid Serif
  • Droid Robot
  • Freedom
  • Fun Raiser
  • Android Nation
  • Green Avocado
  • Recognition

Simply:

TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));

I am author of this library.

Vijay Vankhede
  • 3,018
  • 1
  • 26
  • 46
1

To change the (custom) font of your app globally, have a look at Calligraphy

Simply add Calligraphy to your gradle.build and add the following snippet to your Application.onCreate():

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                        .setDefaultFontPath("fonts/MyCustomFont.ttf")
                        .setFontAttrId(R.attr.fontPath)
                        .build()
        );

and in every Activity add the following:

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

That is all you need to do to change the font globally in your App. Have a look at the docs for more details.

Stefan Medack
  • 2,731
  • 26
  • 32
0
// My example show you how to change fonts into a normal textView or list view

create a fonts folder into your assets dir of android and copy your custom font in that ..
assets/fonts/monaco.ttf

// Font path
String fontPath = "fonts/monaco.ttf";

// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);

// CASE 1 : Inside your list view           
holder.name = (TextView) convertView
                .findViewById(R.id.textView_cityName);

// set name of text in each row 
holder.name.setText(CitiesNames.get(position));

// set the type of font you want to set
holder.name.setTypeface(tf);

// CASE 2 : Inside your text view

TextView tx = (TextView)findViewById(R.id.textview1);
tx.setTypeface(tf);

//vKj
Vinod Joshi
  • 7,696
  • 1
  • 50
  • 51
-1
TextView textView = (Textview) findViewById(R.id.mytext);
Typeface face=Typeface.createFromAsset(getAssets(),
                                      "fonts/Verdana.ttf");
textView.setTypeFace(face);
saman
  • 438
  • 3
  • 8