11

I am trying to use custom font in android. I have written java code as given below.

TextView txt = (TextView) findViewById(R.id.customfont);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Molot.otf");
txt.setTypeface(font);

where I have stored the custom font in "./assets/fonts/" folder.

But, I am getting nullpointerexception on the 3rd line.

Can anybody give me the solution for this issue? Thanks

RATTLESNAKE
  • 3,218
  • 2
  • 21
  • 24

3 Answers3

8

Android supports only TTF font type not OTF. Check this link

"RuntimeException: native typeface cannot be made" when loading font

Community
  • 1
  • 1
DeRagan
  • 22,827
  • 6
  • 41
  • 50
  • 1
    Thanks a Lot! But I tried even using .ttf, it doesn't work yet...and still throws the same exception – RATTLESNAKE Sep 09 '10 at 07:53
  • Please note that the Font name you are entering is case sensitive. If you are still stuck up i probably think it should be a issue with the Font file you are using. Try downloading a different font TTF file and loading it.After a quick googling i found this website which can convert OTF to TTF. May be you can give a try http://onlinefontconverter.com/ – DeRagan Sep 09 '10 at 09:30
  • You can also check this post which has a sample project associated with it... http://stackoverflow.com/questions/3203694 – DeRagan Sep 09 '10 at 09:41
  • Did you download the sample project available with the above link and try running it?..it should work http://github.com/commonsguy/cw-android/tree/master/Fonts/FontSampler/ – DeRagan Sep 09 '10 at 15:00
1

Android supports OTF as well. Your font may be corrupt.

Download delicious font from www.exljbris.com/delicious.html. It is an otf font and it works for me.

Rahul Arora
  • 1,036
  • 9
  • 8
0

Try this
Typeface font = Typeface.createFromAsset(getAssets(), "/fonts/Molot.otf");

tiranodev
  • 943
  • 2
  • 8
  • 15