Question

I am trying to include a customized font in my android application. This is the tutorial that I followed and this seems to have worked for many people: http://tharindudassanayake.wordpress.com/2012/02/25/use-sinhala-fonts-for-your-android-app/

This is how I have tried it:

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/sinhala_font"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="wdhqfndajka"
        android:textSize="25sp" />

</LinearLayout>

Code:

TextView txt = (TextView) findViewById(R.id.sinhala_font);
        Typeface font = Typeface.createFromAsset(getAssets(), "fonts/amal.TTF");
        txt.setTypeface(font);

Unfortunately app crashes (due to native typeface cannot be made) and I get the following in my log cat:

10-27 11:39:14.311: E/AndroidRuntime(28133): FATAL EXCEPTION: main
10-27 11:39:14.311: E/AndroidRuntime(28133): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sinhala/com.example.sinhala.MainActivity}: java.lang.RuntimeException: native typeface cannot be made
10-27 11:39:14.311: E/AndroidRuntime(28133):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
10-27 11:39:14.311: E/AndroidRuntime(28133):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
10-27 11:39:14.311: E/AndroidRuntime(28133):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
10-27 11:39:14.311: E/AndroidRuntime(28133):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
10-27 11:39:14.311: E/AndroidRuntime(28133):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-27 11:39:14.311: E/AndroidRuntime(28133):    at android.os.Looper.loop(Looper.java:137)
10-27 11:39:14.311: E/AndroidRuntime(28133):    at android.app.ActivityThread.main(ActivityThread.java:4517)
10-27 11:39:14.311: E/AndroidRuntime(28133):    at java.lang.reflect.Method.invokeNative(Native Method)
10-27 11:39:14.311: E/AndroidRuntime(28133):    at java.lang.reflect.Method.invoke(Method.java:511)
10-27 11:39:14.311: E/AndroidRuntime(28133):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
10-27 11:39:14.311: E/AndroidRuntime(28133):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
10-27 11:39:14.311: E/AndroidRuntime(28133):    at dalvik.system.NativeStart.main(Native Method)
10-27 11:39:14.311: E/AndroidRuntime(28133): Caused by: java.lang.RuntimeException: native typeface cannot be made
10-27 11:39:14.311: E/AndroidRuntime(28133):    at android.graphics.Typeface.<init>(Typeface.java:238)
10-27 11:39:14.311: E/AndroidRuntime(28133):    at android.graphics.Typeface.createFromAsset(Typeface.java:212)
10-27 11:39:14.311: E/AndroidRuntime(28133):    at com.example.sinhala.MainActivity.onCreate(MainActivity.java:15)
10-27 11:39:14.311: E/AndroidRuntime(28133):    at android.app.Activity.performCreate(Activity.java:4470)
10-27 11:39:14.311: E/AndroidRuntime(28133):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
10-27 11:39:14.311: E/AndroidRuntime(28133):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
10-27 11:39:14.311: E/AndroidRuntime(28133):    ... 11 more

What could possibly be the reason for this? Any help is appreciated!

Was it helpful?

Solution

Are you created a folder called fonts in assets or directly placed in assets folder??if folder not created create a folder and place your font file there I think that is the mistake..In your example he is not created any fonts folder he is directly accessing but you are accessing from fonts folder..

If not fonts folder then change this line to.

Typeface font = Typeface.createFromAsset(getAssets(), "fonts/amal.TTF");

like this..

Typeface font = Typeface.createFromAsset(getAssets(), "amal.TTF");

OTHER TIPS

i got the same exception. in my case i gave a wrong path to my ttf file. i wrote: /font/somefont.ttf i fixed it by removing the first hyphen: font/somefont.ttf

You'll save yourself a lot of trouble, if you're using Android Studio, by placing the "assets/" folder WITHIN "src/main", NOT on the same level as "src/".

In Typeface font = Typeface.createFromAsset(getAssets(), "fonts/amal.TTF"); change to

Typeface font = Typeface.createFromAsset(getAssets(), "fonts/amal.ttf"); that solved for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top