Quick Tip: Customize Android Fonts

Quick Tip: Customize Android Fonts

Tutorial Details
  • Technology: Android SDK
  • Difficulty: Beginner
  • Completion Time: 10 - 15 Minutes

Every Android device comes with a collection of standard fonts: Droid Sans, Droid Sans Mono and Droid Serif. They were designed to be optimal for mobile displays, so these are the three fonts you will be working with most of the time and they can be styled using a handful of XML attributes. You might, however, see the need to use custom fonts for special purposes. We’ll be taking a look at that as well in this quick tip.

Font Style Attributes

In the following section we’re going to examine the different XML attributes that you can use to style components with text. If you wish to follow along, then setup a new Android project in your IDE of choice and open up your main.xml layout file.

Typeface

As stated in the overview, there are three different default typefaces which are known as the Droid family of fonts: sans, monospace and serif. You can specify any one of them as the value for the android:typeface attribute in the XML declaration of a component that supports text styling, such as TextView. Here’s an example of all three typefaces in action:

<?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:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="This is a 'sans' demo!"
            android:typeface="sans"
            />
     <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="This is a 'serif' demo!"
            android:typeface="serif"
            />
     <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="This is a 'monospace' demo!"
            android:typeface="monospace"
            />
</LinearLayout>
Android TextView: Default Android Font Styles

In addition to the above, there is another attribute value named “normal” which defaults to the sans typeface.

Text Style

The android:textStyle attribute can be used to put emphasis on text. The possible values are: normal, bold, italic. You can also specify bold|italic.

<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is bold!"
        android:textStyle="bold"
        />
Android TextView: Customizing Android Font Style

Text Size

android:textSize specifies the font size. Its value must consist of two parts: a floating-point number followed by a unit. Available units are: sp (scaled pixels), px (pixels), dp (density-independent pixels), in (inches), mm (millimeters). It is generally a good practice to use the sp unit so the size can scale depending on user settings.

<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="15sp is the 'normal' size."
        android:textSize="15sp"
        />
Android Textview: Setting Android Font Size

Text Color

The android:textColor attribute’s value is a hexadecimal RGB value with an optional alpha channel, similar to what’s found in CSS and can be in one of the following formats:

  • #RGB
  • #ARGB
  • #RRGGBB
  • #AARRGGBB

You can also reference a color declaration using @color/color_name.

    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="A light blue color."
            android:textColor="#00ccff"
            />
Android TextView: Setting Android Font Color

Text Shadow

You can use three different attributes to customize the appearance of your text shadow:

  • android:shadowColor Shadow color in the same format as textColor.
  • android:shadowRadius Radius of the shadow specified as a floating point number.
  • android:shadowDx The shadow’s horizontal offset specified as a floating point number.
  • android:shadowDy The shadow’s vertical offset specified as a floating point number.

The floating point numbers don’t have a specific unit – they are merely arbitrary factors.

    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="A light blue shadow."
            android:shadowColor="#00ccff"
            android:shadowRadius="1.5"
            android:shadowDx="1"
            android:shadowDy="1"
            />
Android TextView: Adding Android Font Shadow

Using Custom Fonts

Lastly we’re going to examine the process of using custom fonts. We’ll use this font for demonstration purposes. Download it and place the TTF file in the ./assets directory (create it if it doesn’t exist yet).

We’re going to use a basic layout file with a TextView, marked with an id of “custom_font” so we can access it in our code.

<?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/custom_font"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="This is the Chantelli Antiqua font."
            />
</LinearLayout>

Open your main activity file and insert this into the onCreate() method:

        TextView txt = (TextView) findViewById(R.id.custom_font);
        Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");
        txt.setTypeface(font);

The Typeface class contains a static builder method createFromAsset, which takes an AssetManager as its first parameter and a path to the file in the second argument. We’re handing it the default asset manager and the name of the font file because it’s located in the root of the “assets” folder. Once we’ve got an instance of our custom typeface, all that’s left is a call to TextView’s setTypeface() method. Simple, huh? It might also be wise to organize your fonts into a subdirectory if your assets directory is packed with other files.

There are a few potential problems that custom typefaces come with, though. Ellipsizing might not work correctly if the font doesn’t have a glyph for the special ellipsis character and internationalization might not be supported, as your font would have to handle any language that users might input. You’ll also want to keep an eye on the total size of your custom fonts, as this can grow quite large if you’re using a lot of different typefaces.

Android TextView: Using a Custom Android Font

Conclusion

This quick tip has shown you the different options that are available to you for customizing default Droid fonts. You have also learned how to include and use custom typefaces in your application. Just remember to ensure that any custom font you may be using has a license that grants you permission to use it for these purposes!

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.pixelrockit.com Bartosz Oczujda

    I wonder about legal side of using custom fonts in Android apps. Isn't there a special type of license required? Like in the case of @font-face on web pages?

  • Hannes Holste
    Author

    Yes, you'd definitely need the right to redistribute it and obviously it must permit commercial use if you're intending to release a paid app.

    The Droid family of fonts are licensed under the Apache license V2. (http://www.apache.org/licenses/LICENSE-2.0)

  • http://www.ascendercorp.com Bill Davis

    Great article Hannes! My company, Ascender Corp, developed the Droid fonts that are part of the Android platform. These were designed and optimized for the Android mobile platform. As you point out, the Droid fonts provide quite a bit of design flexibility, but they do have limitations with respect to style and character sets.

    Thanks for pointing out the importance of checking the font license before using any custom fonts. Many 'free' fonts have restrictions on commercial use.

    Another important consideration for adding a custom font is quality and readability. How does the font render at on screen? Especially in an application's menus or user interface. Unfortunately very few of the thousands of fonts were designed for the screen – rather they were designed for printing.

    We offer an extensive library of commercial for developers who are looking for a custom font for their application. The Ascender team is known for our expertise in tuning fonts for the screen, and for helping software developers and hardware manufacturers with fonts that are optimized for the environment. This also includes ensuring that the fonts have the proper character sets appropriate for the languages and markets the products will be used in.

    We encourage Android developers to explore the typographic features of the platform. It continues to evolve and improve, and fonts are a very visual and core component of the user experience.

  • Rosy

    Please I need help to change font bigger in my contacts. I have a my touch 3g which is android.

    Thank You for your help in advance

  • Nishant

    hi!

    i face problem during customize font in android.

    i use net beans.

    There is no effect when i used the above code

    please help me.

  • Jon

    Is it possible to set the custom font in the xml, like when picking an included font? Styling an application in XML doesn’t make much sense if one has to add the font in the code anyways.

    • Hithen

      Hi Jon,
      I am just repeating your question. Do u find any solution for customizing fonts in XML instead of writing code for all views. I have more no.of views in my activity. How can we do this?

  • vasu

    Hi,

    I have one doubt? is it possible to add the shadow to image dynamically instead of text?

  • Karla

    Why hard-code the size? Why not make it user selectable? Or at least have 3-5 sizes — XS – S – M – L – XL that correlate to a font size?

  • ria

    thanks a lot. DO you also have recommendation for font sizes according to visual design viewpoint? What should be
    biggest font size for buttons and what should be smallest font sizes..for notification. Do you have any guidelines in this area?

  • http://robaldred.co.uk Rob

    Great article,
    Do you know if it’s possible to define custom fonts in a themes.xml and styles.xml file instead of in code, all my view stying is done in xml it seems a bit odd to have to go into application code to set the font for a textview.

    I was hoping you might be able to do something like:

    ‘styles.xml’:

    @color/text_dark
    20sp
    @assets/fonts/somefont.ttf

    Cheers
    Rob

  • Ted

    Great article!

    I am about to make an app also for Arabic language. And as I can´t see any support for bidi languages in Droid fonts I have a Q. How about mixing with character sets like bidi langugages with westen TTF (left to right). Is there a fallback solution if the unicode doesnt exist in one font set it will look into another fontset? And will the rendering will manage both types?
    Thanks for a great article!

    B.R
    Ted

  • http://twitter.com/boubakr92 Boubakr Nour

    It’s work with all fonts’ types !?

  • Ketan

    Nice article !

    Cheers,

    Ketan

  • Bear

    Great article. I’ve always wondered how to set custom fonts. Many thanks!

  • Alan

    I’d like to try this on my wife’s Samsung Intercept, but don’t know how to get started.

    How do I find and edit the main.xml layout file?

    Is there an app to do this?

    Is there a way to save & restore the original in case I mess up.

    Does the phone have to be rooted?

    Thanks, Alan

  • http://www.seaurchinfacts.info sea urchin facts

    Thanks you. Very good.

  • Rob

    Your article gives me hope as I’m about to go back to my old iPhone .you don’t tell how to change the font size. I have a htc desire s and I can’t read the fonts unless I dig out my glasses.
    I want at least size 25 and bold, how do I do it, where do I go on the phone, I’ve searched set up, settings, personalize phone.
    Your help would be appreciated.
    Rob.

  • http://stratospherecs.com James

    This post is a Programming related post. All commentors that need settings changed on individual phones need to check their phone manual. The “Settings” application in all Android devices should have what you seek.

  • ana

    i need urdu fonts that support android??

  • Imam Musa

    Please i need someone to guide me in insert a file into Android application which will contain a video and image to illustrate or Tutorial.please i need it urgently for my major project

  • MICK

    Could you please tell me how i make the font on my htc larger i find it difficult to see them.
    cheers mick

    • http://www.treehouseprojects.ca Treehouse Projects

      This is for developers. I think your problem is just as a general user, you are having difficulty seeing the text in your applications. I think there are apps in the Play store which can help you out with this issue. It is not uncommon.

  • http://www.treehouseprojects.ca Treehouse Projects

    Thank you! Great crash course on fonts :)

  • B.Rabbani

    how to change text size in custom fonts ???

  • Terry

    Thanks, this information is just what I needed! It’s hard to find the XML settings for fonts in the Android documentation and this spells it out nicely, with useful examples.

  • http://sharad-pathology.blogspot.com/ sharad

    By mistake I deleted DroidSansFallback.ttf which was required for displaying Gujarati language font. Even after replacing that file, I Gujarati language characters are not displayed now. I am using galaxy nexus on 4.2.1

  • Johnson

    Create custom font on Android application using font encoder,
    http://www.keepautomation.com/font_tools/android_barcode_font_encoder.html