Custom Fonts in Android
Asked Answered
A

7

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.

Ackler answered 8/7, 2010 at 12:47 Comment(4)
Check this post: https://mcmap.net/q/57814/-how-to-change-the-font-on-the-textviewNarbada
Check this one also : https://mcmap.net/q/57126/-using-a-custom-typeface-in-androidEmplane
#9030704Speedwriting
@ASP it would not be needed anymore since Google has already provided an option with Android O to set custom fonts :) Check this out medium.com/@pareshmayani/…Ackler
E
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.

Embosom answered 8/7, 2010 at 12:50 Comment(5)
is there a way to apply this to all text app wide rather than individual text views?Eichmann
Can you set the font for a control via the xml layout?Mitch
Yes and no. You cannot do it out of the box... but you could extend TextView to add such functionality.Embosom
@LukeBatley One possible solution is posted here #17648115Dhaulagiri
@Dhaulagiri but how would you apply, for instance, Verdana font using your solution?Embosom
M
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" />
Margaret answered 7/8, 2013 at 6:53 Comment(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)Delhi
G
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!!

Gula answered 27/10, 2015 at 12:20 Comment(0)
S
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.

Spirited answered 25/6, 2015 at 18:18 Comment(0)
A
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.

Actinometer answered 22/4, 2016 at 8:58 Comment(0)
P
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
Puncture answered 6/2, 2014 at 9:26 Comment(0)
B
-1
TextView textView = (Textview) findViewById(R.id.mytext);
Typeface face=Typeface.createFromAsset(getAssets(),
                                      "fonts/Verdana.ttf");
textView.setTypeFace(face);
Blackwell answered 9/2, 2015 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.