64

I am attempting to use a custom font for a TextView on Android, following the guide here. Using the same font, same code, same everything, I get this in adb logcat:

W/dalvikvm(  317): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime(  317): FATAL EXCEPTION: main
E/AndroidRuntime(  317): java.lang.RuntimeException: Unable to start activity  ComponentInfo{org.evilx.quacklock/org.evilx.quacklock.MainActivity}:             java.lang.RuntimeException: native typeface cannot be made
E/AndroidRuntime(  317):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime(  317):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(  317):        at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(  317):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(  317):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  317):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  317):        at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(  317):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  317):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  317):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  317):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(  317):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  317): Caused by: java.lang.RuntimeException: native typeface cannot be made
E/AndroidRuntime(  317):        at android.graphics.Typeface.<init>(Typeface.java:147)
E/AndroidRuntime(  317):        at android.graphics.Typeface.createFromAsset(Typeface.java:121)
E/AndroidRuntime(  317):        at org.evilx.quacklock.MainActivity.onCreate(MainActivity.java:24)
E/AndroidRuntime(  317):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(  317):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime(  317):        ... 11 more
W/ActivityManager(   59):   Force finishing activity org.evilx.quacklock/.MainActivity
W/ActivityManager(   59): Activity pause timeout for HistoryRecord{43e80368 org.evilx.quacklock/.MainActivity}
D/dalvikvm(  247): GC_EXPLICIT freed 711 objects / 53160 bytes in 20922ms

I'm using the font Molot.otf, which was successfully used in one of the blogs. I'm also using predator.ttf, another custom font but in TrueType format.

Relevant code:

public class MainActivity extends Activity {
    // Called when the activity is first created.
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Molot.otf");
        TextView tv = (TextView) findViewById(R.id.CustomFontText);
        tv.setTypeface(tf);
    }
}

and

<?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/CustomFontText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="Here is some text.">
    </TextView>
</LinearLayout>

What would be causing this? It worked for the people in the blogs, so why not me? Did something significant change in the API that's preventing me from doing this?

BSMP
  • 4,596
  • 8
  • 33
  • 44
TheLQ
  • 14,830
  • 14
  • 69
  • 107
  • 6
    I got this error and wondered what it meant, then I figured out that it happened because **it couldn't find the file to load**. Despite this might be unrelated to this post, which I haven't even read, I post it here because it's the only result I get on google searching for this error, so I guess it's worth noticeing anyway. – o0'. Jul 05 '10 at 14:36
  • 1
    I think that was the problem in my situation as well, but I'm not sure. I ended to up doing lots of tinkering and somehow got it to work – TheLQ Jul 05 '10 at 20:14
  • 2
    Apparently Android also doesn't like the situation where the font filename doesn't match the (embedded) name of the font. I got the exact same exception after simply renaming the ttf file. – mvds May 23 '11 at 23:14
  • 6
    I had the same issue as Lohoris. I was refactoring my code and forgot to include the actual font file into my project (in my case, in Assets/fonts). – Sandy Nov 24 '11 at 02:26
  • 2
    @lohoris thanks, that was my problem. i'd +1 you if you had written it as an answer :) – Jeffrey Blattman Jan 27 '12 at 02:45

9 Answers9

48

Android does not support OpenType (OTF), only TrueType (TTF), so your Molot.otf font probably will not work. I wrote both of those blog posts you link to in your opening sentence (the one is a pirated copy of the other), and they do not use Molot.otf.

(BTW, I somewhat repaired the formatting of that post -- AndroidGuys keeps changing WordPress hosts, and so my older posts are terribly broken in terms of formatting).

EDIT: As stated in the comments, Android DOES now support OTF.

jonstaff
  • 2,672
  • 2
  • 19
  • 18
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    But why would a working TrueType font fail then? Are their different types of TTF's out there? – TheLQ Jun 11 '10 at 15:28
  • 1
    @Lord.Quackstar: Sorry, I thought the TrueType font was working for you. As the blog posts indicate, Android doesn't like all fonts. It used to be a quiet failure, but that was two years ago -- perhaps it crashes now. Also, make sure the font is in the directory you stipulate in your assets area. Nowadays, you can also load a font off the filesystem -- see the static `createFromFile()` method, so you could give that a try. You can download the HandmadeTypewriter font I used in that post, as I know that works, so if that fails for you, it's not the font but something else with your environment. – CommonsWare Jun 11 '10 at 15:49
  • HandmadeTypewriter and a separate program I found to list the assets directory all failed. Currently downgrading to 2.1. Thanks – TheLQ Jun 11 '10 at 17:11
  • Found out it was a bug in the netbeans plugin I was using. However apparently in 2.1-u1, I can open Molot.otf. Might want to make note of that. – TheLQ Jun 12 '10 at 14:23
  • @Lord.Quackstar: That's certainly cool (the OTF working part)! I'll have to run some experiments sometime. Thanks for the note! – CommonsWare Jun 12 '10 at 14:30
  • 11
    This answer is out-dated.. I have successfully rendered OTF fonts using android's Typeface – Tyler Jun 21 '12 at 15:58
27

I also get the same error and I have solution follow.

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Molot.otf")

you must put fonts/Molot.otf in assets/fonts folder in your Eclipse.

after you can Run it.

if you can not Run it successfull you can send proplem via

taskinoor
  • 45,586
  • 12
  • 116
  • 142
phuongnv
  • 271
  • 3
  • 2
  • 1
    My problem was just a typo, but this answer helped to find out it. – Felipe Nov 29 '12 at 19:55
  • I had the same error "native typeface cannot be made" and it was a typo. I was trying to access the font from "/fonts/font.otf" instead of "fonts/font.otf". – speedynomads Jul 22 '13 at 10:31
10

Unfortunately, the typeface cannot be made error is not very specific, and it can be the result of many things going wrong. It's important to check for two things:

  • The first and most important: The file is found!
  • The font is valid in your device.

The best way is to change your font file for a known valid font file.
If it fails, then it's the first problem.
If not, it's the second, so you will have to deal with FontForge or look for another font.

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
mamuso
  • 3,117
  • 3
  • 30
  • 32
9

check font's name and extension. it is case sensitive & probably all caps. eg.

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/MOLOT.OTF")
marcieng
  • 1,021
  • 9
  • 14
6

FYI. My reason for the crash is some reason caused by Eclipse. All I did is just cleaning the project and ran again, then it works.

Firstly, I tried the custom font in my test project which I use to try some new functions.It worked on the very first time. But it didn't work on the project i'm working on until I did as above.

So try as many methods as you can.

Michelle
  • 1,097
  • 8
  • 18
4

You should edit your font with 'Fontlab' Software in binary mod.

ali shekari
  • 740
  • 1
  • 11
  • 26
4

@deng his answer worked for me":

check font's name and extension. it is case sensitive & probably all caps. eg.

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/MOLOT.OTF")
Prahalad Gaggar
  • 11,389
  • 16
  • 53
  • 71
1

Android does support OTF files for Typefaces, if you're facing this problem, make sure that you're setting the right path for the font, for example, if you have the file fontname.otf, put it in a folder fonts inside assets folder and create the typeface like follows :

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/fontname.otf");

(path argument should not start with "/") and the file name should not include special characters or a "-" and should be in lowercases

Alaa Eddine Cherbib
  • 1,062
  • 14
  • 17
0

Android does support OTF files for Typefaces, if you're facing this problem, make sure that you're setting the right path for the font.put font into folder fonts inside assets folder and create the typeface as below :

Typeface typeface = Typeface.createFromAsset(getAssets(), "font/StencilStd.otf");
TextView text = (TextView) findViewById(R.id.textView);
text.setTypeface(typeface);
Hitesh Raviya
  • 129
  • 6
  • 13