An Android device comes with 3 basic fonts – Droid Sans, Droid Sans Mono, and Droid Serif. These are sufficient for every day use, but for your custom apps you will most probably want to use your own font. This post will show you how you can do so.

First Steps

First step is to pick a font that you want to use. For this example, we’ll be using BPReplay, an OpenType font that I stumbled across here.

bpReplay

Hot looking font! In order to access our font easily, we need to bundle it with our application in a way that our code can subsequently load it. To do this, we create a Fonts folder in our assets directory and copy our font there.

fontsFolder

That’s it for the setup, now on to the code.

The Code

To access our custom font, we use the Typeface class in the Android SDK to create a typeface that Android can use, then set any display elements that need to use our custom font appropriately. To demonstrate, we’ll create two text views on our main screen, one using the default Android Sans font, and the other using our BPReplay font. The layout is below.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/DefaultFontText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="Here is some text." />
    <TextView
        android:id="@+id/CustomFontText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="Here is some text.">
        </TextView>
</LinearLayout>

The code to load and set the custom font is straight forward as well, and is shown below.

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

        Typeface tf = Typeface.createFromAsset(getAssets(),
                "fonts/BPreplay.otf");
        TextView tv = (TextView) findViewById(R.id.CustomFontText);
        tv.setTypeface(tf);
    }
}

You can see on line 8 where we instantiate a new Typeface object from our font definition file. On lines 9 and 10, we set the text view to use our custom font. The result us shown below.

customFont1

Sure enough, we have a different font on the screen. Using another freeware font called Molot, our font goodness becomes even more apparent.

customFont2

  29 Responses to “Android Development – Using Custom Fonts”

  1. [...] itself as being about “Coding and managing in the .NET space”, recently ran the excellent Android Development – Using Custom Fonts. You’ve always been able to use your own fonts in your own apps, but the how-to coverage has been [...]

  2. [...] itself as being about “Coding and managing in the .NET space”, recently ran the excellent Android Development – Using Custom Fonts. You’ve always been able to use your own fonts in your own apps, but the how-to coverage has been [...]

  3. [...] itself as being about “Coding and managing in the .NET space”, recently ran the excellent Android Development – Using Custom Fonts. You’ve always been able to use your own fonts in your own apps, but the how-to coverage has been [...]

  4. [...] itself as being about “Coding and managing in the .NET space”, recently ran the excellent Android Development – Using Custom Fonts. You’ve always been able to use your own fonts in your own apps, but the how-to coverage has been [...]

  5. [...] itself as being about “Coding and managing in the .NET space”, recently ran the excellent Android Development – Using Custom Fonts. You’ve always been able to use your own fonts in your own apps, but the how-to coverage has been [...]

  6. [...] itself as being about “Coding and managing in the .NET space”, recently ran the excellent Android Development – Using Custom Fonts. You’ve always been able to use your own fonts in your own apps, but the how-to coverage has been [...]

  7. [...] itself as being about “Coding and managing in the .NET space”, recently ran the excellent Android Development – Using Custom Fonts. You’ve always been able to use your own fonts in your own apps, but the how-to coverage has been [...]

  8. [...] itself as being about “Coding and managing in the .NET space”, recently ran the excellent Android Development – Using Custom Fonts. You’ve always been able to use your own fonts in your own apps, but the how-to coverage has been [...]

  9. [...] itself as being about “Coding and managing in the .NET space”, recently ran the excellent Android Development – Using Custom Fonts. You’ve always been able to use your own fonts in your own apps, but the how-to coverage has been [...]

  10. Ooh, thanks. Apparently .otf files work. I used a .ttf file and it worked, too.

  11. [...] itself as being about “Coding and managing in the .NET space”, recently ran the excellent Android Development – Using Custom Fonts. You’ve always been able to use your own fonts in your own apps, but the how-to coverage has been [...]

  12. I have a question and it would be great if you could help me out.

    I have an Android phone, and I would like to add “Persian/Farsi” font so I can browse websites that are composed in Farsi.

    could you please let me know how it can be done?
    I must admit that i am very illiterate about Android.

    cheers,

    A.J.

  13. Thanks! I know this post is almost a year old but it helped me today. This worked perfectly for me using a .tff file. Thanks again!

  14. u are so good at Android software system, I would like to ask you about Unicode I try to past the unicode according to your instruction as above but it still display incorrectly

    you can check here

    I hope you will give any solution to view correctly of my unicode language

    Thank you

    dara sun
    Cambodia

  15. Where can I find those fonts?
    I need Tahoma and Dingbats.
    Thx in advance for help!
    lessa

  16. thanks, it helped me today

  17. For some reason this works only for API levels 3 to 7

    It dosn’t work for API level 8 and above.

  18. Thank you!!!

  19. does android support urdu fonts? if yes then kindly tell me the name of that urdu font?

  20. Thannkkkk uuuuuuuuuuu
    Cheeeetaaaaaaaa

  21. thanks for your quick and efficient tutorial :)

  22. [...] The rest of my Player code relies on more callbacks for MediaPlayer (like OnSeekCompleteListener and OnCompletionListener), a headless speech recognizer implementation (a bunch more callbacks), a really clean swipe gesture implementation for "flipping the page" without having to go through the speech-recognition rigamarole (you guessed it, more callbacks). And a button that toggles/indicates listening/matching state and uses some icons from the free version of GLYPHICONS (note, Glyphish is another stylish option I've used for other projects). Handler.postDelayed(someCoolRunnable, milliseconds) takes care of pausing the video when an animation "page" is complete. The PlayerActivity is also wrapped up in a FrameLayout with a custom font, to boot (code tips for custom fonts). [...]

  23. i hope android suport font khmer soon….
    if how to inlude font this OS , please tell me ucz i want my people
    easy use this OS. thank u advance
    u_eang@yahoo.com

  24. I’m Getting Error…. Don’t What’s the Problem is…

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

   
© 2011 Musings of the Bare Bones Coder Suffusion theme by Sayontan Sinha