24

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?

hoss
  • 2,430
  • 1
  • 27
  • 42
Jojo
  • 490
  • 1
  • 7
  • 22

4 Answers4

53

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!

13

Create Assets folder Right click on app->>new->>Folder->>AssetsFolder like below image

enter image description here

Put your font inside this folder by just copy and paste. and use below code for example..

Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "YourFontName.ttf");
setTypeface(tf);
kundan roy
  • 1,936
  • 1
  • 18
  • 21
0

Simply follow this path:

File > New > folder > assets Folder

Here App must be selected before creating folder.

For more information see this answer

Community
  • 1
  • 1
Md. Sajedul Karim
  • 6,749
  • 3
  • 61
  • 87
-1

The assets folder should be placed under the root of the project. See here for more examples.

Lior Ohana
  • 3,467
  • 4
  • 34
  • 49
  • 2
    Not according to this post http://stackoverflow.com/questions/18302603/where-to-place-assets-folder-in-android-studio – Jojo Oct 07 '13 at 13:19
  • 1
    Weird. I need to check this stuff. Either Google enhanced the getAssets to search for both in the root and under src/main or the post you gave is missing something. I'll check and update. – Lior Ohana Oct 07 '13 at 13:22
  • 1
    @LiorOhana that post seems to refer to eclipse, not to Android Studio – pconcepcion Apr 24 '14 at 10:12