9

I'm trying to add a font to my Android project, so I can use it via XML. Unfortunately, I'm getting this error message:

Error:Error: The file name must end with .xml

I followed every step of the official Android tutorial and somehow this error occurs and I can't fix it.

Here is this my error

halfer
  • 19,824
  • 17
  • 99
  • 186
thelearner
  • 1,440
  • 3
  • 27
  • 58

2 Answers2

8

You can't add font.ttf file in Drawable folder

you need to create Assets folder in android you can create Assets folder like below image in Android studio

enter image description here

than create a font folder in Assets folder and put your custom font.ttf file in this folder like below image

enter image description here

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • 4
    "Android 8.0 (API level 26) introduces a new feature, Fonts in XML, which lets you use fonts as resources. You can add the font file in the res/font/" ? – thelearner Oct 14 '17 at 14:38
  • @7Learner you didn't mention in question about api level 26 please read this https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html#using-support-lib – AskNilesh Oct 15 '17 at 05:45
  • @7Learner check this link it may help you https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html#using-supporthttps://stackoverflow.com/questions/44505878/using-font-in-xml-in-api-26-lib and this – AskNilesh Oct 15 '17 at 05:47
  • man!! if you create the assets directory like-- res ->right click -> new -> Directory- > name it "assets". Then if you put any file in that assets folder you wont be able to read that file with asset manager. took me 3 hours to realise this thing. Always create it like res ->right click -> new -> Folder -> Assets Folder – Saurav Mir Jun 06 '20 at 07:06
0

Put the font in your project in assets/fonts directory

then in your code to get the Typeface:

Typeface myNewTypeface = Typeface.createFromAsset(context.getAssets(), "fonts/the_new_font.ttf");
from56
  • 3,976
  • 2
  • 13
  • 23
  • 1
    "Android 8.0 (API level 26) introduces a new feature, Fonts in XML, which lets you use fonts as resources. You can add the font file in the res/font/" ? – thelearner Oct 14 '17 at 14:42
  • I did not test this new feature. My applications must also work in earlier versions of Android and therefore I can not use that. – from56 Oct 14 '17 at 14:56