Assets folder in Android Studio?

Jojo picture Jojo · Oct 7, 2013 · Viewed 35.5k times · Source

Im trying to use a custom font, and I've read that I suppose to place the fonts in assets/fonts. I'm using Android Studio and it doesn't seem like I have a assets folder. So I created one. But my app crashes when I place the assets folder in src/main. Im using this code to load my fonts.

Typeface fontRegular = Typeface.createFromAsset(getAssets(), "fonts/DroidSans.ttf");
Typeface fontBold = Typeface.createFromAsset(getAssets(), "fonts/DroidSans-Bold.ttf");

myDeviceModelTxt.setTypeface(fontRegular);

What am I doing wrong?

Answer

agreeingly-ascyphous picture agreeingly-ascyphous · Mar 30, 2014

I am not sure if there has been any bug fixes since this was asked, but I am using the current structure for a project in Android Studio 0.5.2:

root-module
|--.idea
|--app
|----build
|----src
|------main
|--------assets
|----------SomeFont.ttc
|----------AnotherFont.otf
|--------java
|----------source code here
|--------res
|------AndroidManifest.xml
|----build.gradle

And then obtain it by calling

Typeface.createFromAsset(mContext.getResources().getAssets(), "SomeFont.ttc");

Word of caution though, there is a bug (https://code.google.com/p/android/issues/detail?id=9904) that prevents typefaces from being garbage collected properly. Use a singleton!