1

I've read every SO post regarding use of fonts in Android and I still can't get mine to work. I'm getting the following exception:

java.lang.RuntimeException: native typeface cannot be made

Here's my code (this class and static method is taken from here):

public class SafeTypefaces {

    private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();

    public static Typeface get(Context c) {

        final String assetPath = "fonts/robotobold.ttf";

        synchronized (cache) {
            if (!cache.containsKey(assetPath)) {
                try {
                    AssetManager assetManager = c.getAssets();
                    Typeface t = Typeface.createFromAsset(assetManager, assetPath); //Throws exception on this line!
                    cache.put(assetPath, t);
                } catch (Exception e) {
                    L.p("Could not get typeface '" + assetPath + "' because " + e.getMessage());
                    return null;
                }
            }
            return cache.get(assetPath);
        }
    }
}

The font is in the assets/fonts folder:

enter image description here

(this is still Eclipse. I know, I know).

Things I've tried:

  • I've tried moving the font to the root of the assets folder and updating the assetPath.
  • I checked folder and file permissions.
  • I tried using the application context instead of the activity context I'm getting.
  • I tried using different font files (one send via email, another downloaded from the Internet).
zundi
  • 2,361
  • 1
  • 28
  • 45

1 Answers1

1

As I was creating this question I ran into the solution. I've spend a few hours on this so I guess its worth keeping.

My problem was that I was working on a library project. According to this (Android Library assets folder doesn't get copied), assets in library projects aren't available to the main project, which is why the font file wasn't being found. (Again, this project is still using Eclipse. Possibly not the case using Gradle+AS).

The solution is to copy the fonts into the main project, and voila!

Community
  • 1
  • 1
zundi
  • 2,361
  • 1
  • 28
  • 45
  • 1
    "Possibly not the case using Gradle+AS" -- correct. Assets in library modules in Android Studio or other Gradle-based Android builds are included in the final APK. – CommonsWare Jun 15 '16 at 17:23