11

I'm trying to use a custom font on elements of a listview. So in my ListViewAdapter constructor I have:

private Context context;
private List<Project> projects;
private Typeface tf;
public ListViewAdapter(Context context, int resource, List<Project> projects) {
    super(context, resource, projects);
    this.context = context;
    this.projects = projects;
    tf = getTypefaceForListItems();
}

public Typeface getTypefaceForListItems()
{
    return Typeface.createFromAsset(context.getAssets(), "fonts/OpenSans-Regular.ttf");
}

In getView method I apply this typeface to a textview. The problem is that I get a Runtime exception: native typeface cannot be made and I don't understand what seems to be the problem.

Here's the stacktrace:

java.lang.RuntimeException: native typeface cannot be made
            at android.graphics.Typeface.<init>(Typeface.java:175)
            at android.graphics.Typeface.createFromAsset(Typeface.java:149)
            at com.maxcode.clientcheck.ProjectListAdapter.getTypefaceForListItems(ProjectListAdapter.java:41)
            at com.maxcode.clientcheck.ProjectListAdapter.<init>(ProjectListAdapter.java:25)
            at com.maxcode.clientcheck.ProjectListActivity$GetProjectsAsyncTask.onPostExecute(ProjectListActivity.java:154)
            at com.maxcode.clientcheck.ProjectListActivity$GetProjectsAsyncTask.onPostExecute(ProjectListActivity.java:91)
            at android.os.AsyncTask.finish(AsyncTask.java:631)
            at android.os.AsyncTask.access$600(AsyncTask.java:177)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

Also I wanted to mention that all my fonts are in src/main/assests/fonts folder.

Alin
  • 1,044
  • 6
  • 20
  • 42
  • 1
    possible duplicate of ["Native typeface cannot be made" only for some people](http://stackoverflow.com/questions/12766930/native-typeface-cannot-be-made-only-for-some-people) – njzk2 Nov 18 '13 at 14:12
  • (also please google your exception before asking a new question, or, if you don't find your answer in the previously asked questions, indicate all that you have tried.) – njzk2 Nov 18 '13 at 14:13
  • I've tried the solution indicated in the link - but the exception persists. I've also googled for an answer but no luck - that's why I've posted this question! ;) – Alin Nov 18 '13 at 14:17
  • 2
    I understand your position. But as long as you don't indicate what you have attempted so far, what i can only see is that the question is very similar to the one I indicated and a dozen others, and therefore the answer should be the same as well. – njzk2 Nov 18 '13 at 14:23

3 Answers3

12

One problem that's overlooked (by Android Studio users) is that the "assets/" folder is NOT on the same level as "src/". It is INSIDE "src/main/"

Because I didn't see this earlier, I spent two hours last night, and an hour this morning just trying to change the font in my navigation drawer.

I believe this is the same reason why pixlUI and calligraphy libraries did not work for me.

wsgeorge
  • 1,928
  • 17
  • 20
0

I do my fonts like this: (I declare them in my MainActivity)

public static TypeFace FONT_HEADINGS;

Then I set them up in onCreate()

FONT_HEADINGS = Typeface.createFromAsset(this.getAssets(), "MyriadPro-Cond.otf");

At this point, I can reference them throughout my app as:

sampleTextView.setTypeFace(MainActivity.FONT_HEADINGS);

Edit:

"src/main/assests/fonts"

the "assets" folder should not be under the "src" folder. It belongs in the at the same level as the src, res, etc.

William Riley
  • 878
  • 8
  • 13
  • I don't think this is the problem because - the name and extension of the font are how I've added them in my code: OpenSans-Regular.ttf Thanks! – Alin Nov 18 '13 at 14:20
  • 1
    Great - this could work too...but the problem for me seems to be that when calling Typeface.createFromAsset(this.getAssets(), "MyriadPro-Cond.otf"); I get a RuntimeException: native typeface cannot be made - and the cause seems to be that getAssets returns null ... because when I call ex.getCause() in the catch clause it said simply...null. – Alin Nov 18 '13 at 14:39
  • 1
    From what I understand - because Android Studio started using the Gradle build system - the assets folder should be under the "src/main" folder. -http://stackoverflow.com/questions/18302603/where-to-place-assets-folder-in-android-studio – Alin Nov 18 '13 at 14:49
  • Was unaware you werent using ADT in Eclipse. – William Riley Nov 18 '13 at 14:50
  • Isn't android studio in Beta? Id avoid it until its stable. Less headaches. – William Riley Nov 18 '13 at 14:51
  • :)) In this case I can't avoid it! I guess I have to find a solution for this and still using Android Studio – Alin Nov 18 '13 at 14:54
  • Lol, just checking because in your question it says "assests" :) – William Riley Nov 18 '13 at 16:03
  • @William Better Create all your TypeFace objects in Application class as in most cases we have to use same typeface through out the project and its will consume high memory if we create it in each activity. Or correct me if i am wrong. – Gem Feb 06 '15 at 12:55
-1

OpenSans-Regular.ttf should be under fonts folder .

folder structure should be like this :

assets-> fonts ->OpenSans-Regular.ttf

i think by mistake you have added under assets folder
Trupti
  • 284
  • 5
  • 7