Where should I place font file in Android resources?-ThrowExceptions

Exception or error:

Where in res folder should I put my font file (TTF)?

How to solve:

You can create the font in asset folder (i.e asset/fonts/roboto.ttf).

Then, create an appropriate class for your TextView:

// RobotoFont class
package com.my.font;
public class RobotoFont  extends TextView {
    public RobotoFont(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public RobotoFont(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public RobotoFont(Context context) {
        super(context);
    }
    public void setTypeface(Typeface tf, int style) {
        if (style == Typeface.BOLD) {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(),    "fonts/Roboto-Bold.ttf"));
        }
        else if(style == Typeface.ITALIC)
        {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Italic.ttf"));
        }
        else
        {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf"));
        }
    }
}

and finally, update your layout:

//main.xml
//replace textview with package name com.my.font.RobotoFont
<com.my.font.RobotoFont
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingBottom="2dip" />

###

Using Custom Font

First step is to pick a font that you want to use.

Second create a Fonts folder in your assets directory and copy your font there.

enter image description here

NB: you can put your font everywhere on asssets folder but this is the way that i do!!

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

To access your custom font, you have to 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 your custom font appropriately. To demonstrate, you can for exemple create two text views on your main screen, one using the default Android Sans font, and the other using your custom 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 the result:

enter image description here

###

Not in the res folder, but anywhere in the assets folder. Then you can use the createFromAsset static method from Typeface:

http://developer.android.com/reference/android/graphics/Typeface.html#createFromAsset%28android.content.res.AssetManager,%20java.lang.String%29

###

You can create fonts folder in resources and use it directly in xml, since Android O.
Check out new Android Ofonts feature.

###

As of Android Studio 1.5.1 you can:

  1. Right click on your app directory
  2. New > Folder (this is near the bottom of the list and is easy to miss) > Assets Folder
  3. In most cases you can leave the folder location as the default > Click Finish
  4. Move your files into the newly created assets folder

Leave a Reply

Your email address will not be published. Required fields are marked *