20

Android O introduces a new feature, Fonts in XML, which lets you use fonts as resources. And I am creating the font folder in resource file as it provided by Android developer, but the problem is that I am getting error that file name must end with .xml while using the font folder which is provided in Android O release.

Please check the below layout for it.

 <TextView
   android:id="@+id/txtMsgCount"
   android:layout_width="wrap_content"
   android:layout_height="16dp"
   android:background="@drawable/msg_count"
   android:gravity="center"
   android:text="123"
   android:fontFamily="@font/Montserrat_Regular" ////IT IS MY FONT STYLE
   android:textColor="@android:color/white"
   android:textSize="10sp" />

And please check the font folder which I have created in res with Montserrat_Regular.otf file

My font folder with font style

I am getting the following error while using the above, which are as follows:

Error getting

I have searched about it but did not get expected result, please check:

1. First Link
2. Second Link
3. Third Link
4. Forth Link

And the Font resource file is not creating as I am clicking the Right-click the font folder and go to New > Font resource file. But not getting the option of the Font resource file, please check below:

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103

7 Answers7

10

Font resources are not fully supported by the Android Studio 2.3.3 build chain. Full support comes with Android Studio 3.0+ and its associated Android Plugin for Gradle version. At that point, you will be able to create a font/ resource directory that goes alongside layout/, menu/, etc.

IOW, hold off on this until you are ready to upgrade to Android Studio 3.0.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Apparently "canary" has been renamed to "preview" and the Android studio build has been relabeled to "beta": http://tools.android.com/download/studio/canary/latest – M66B Aug 11 '17 at 11:20
  • Means there is no other way on 2.3.3..I am also using the `kotlin` whose version `3.0 Beta `..I think it will run on that...But my problem is not resolved because i need to do work on `Andorid `..Thanks for input..:) – Ravindra Kushwaha Aug 11 '17 at 11:22
  • @M66B: They must have bumped Android Studio 3.0 up to Beta 1 while I was asleep. Last I knew, they were on Canary 9 or so. – CommonsWare Aug 11 '17 at 11:24
  • @RavindraKushwaha: "Means there is no other way on 2.3.3" -- you're welcome to try the 3.0 beta version of the Plugin with AS 2.3.3, but I strongly suspect that it will not work. – CommonsWare Aug 11 '17 at 11:25
  • @CommonsWare it looks more structural, since the "canary" page refers to a "preview" page now. This was changed yesterday (CET) or earlier. Although, on the other hand this page suggests otherwise: https://developer.android.com/studio/archive.html – M66B Aug 11 '17 at 11:26
  • 1
    @CommonsWare Just checked, using the 3.0.0-beta1 version of the Gradle plugin does not work with AS 2.3.3. Gradle sync fails with `Error:This Gradle plugin requires Studio 3.0 minimum`. – SpaceBison Aug 30 '17 at 07:41
4

I've lost an hour on this error, using AS 3.0 beta2, Gradle 4.x and the correct build-tools.

Turned out I had a copy of the fonts in a folder called "fonts". This folder doesn't show up in the Android project view. And reading the error I didn't catch the 's'. Just a small reminder for you readers!

halfer
  • 19,824
  • 17
  • 99
  • 186
PieterAelse
  • 3,578
  • 2
  • 23
  • 33
  • 1
    Hi there. A number of folks in our community sometimes say that every time they see gendered assumptions about software engineers, they worry about people feeling excluded. I wonder, could you try to avoid adding male-oriented greetings and pronouns in your posts, so as to make for a more welcoming environment? Thank you. – halfer Sep 01 '17 at 00:31
  • Indeed, that was my case as well. I wonder why it was duplicated. – arslancharyev31 Sep 28 '17 at 20:23
  • In my case, it was a wrongly named values directory. So just scan your structure for a bad name. – VipulKumar Apr 06 '20 at 17:56
3

I just had the same problem and while searching for an answer, I came here on this thread. I've found a solution from deverloper.android.com.

  1. Right click 'res' folder in Android Studio

  2. Choose 'new' > 'Android resource directory'

  3. Name it 'font'

  4. Inhere you should place your font (.ttf) files WITH ONLY a-z 0-9 and _ characters.

  5. Set your font (mine is called 'alegreyaregular.ttf') this way:

Typeface typeface = ResourcesCompat.getFont(this, R.font.alegreyaregular);
TextView textView.setTypeface(typeface);
Community
  • 1
  • 1
Kees de Vriesch
  • 124
  • 2
  • 14
  • 2
    Hey!! Look at that. I was already doing what all the other answers were suggesting but it still was not working. This is the only thing that worked for. – uesports135 Jun 09 '18 at 20:37
2

I had exactly same error file name must end with .xml I used these settings and problem magically resolved.

Use these setting in Project Level Gradle :

compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
    applicationId "com.company.app"
    minSdkVersion 21
    targetSdkVersion 26
...
}

Use these setting in Application Level Gradle :

classpath 'com.android.tools.build:gradle:3.0.1'

Android Stidio :

Android studio version 3.0.1.

Cheers!

Ali Akram
  • 4,803
  • 3
  • 29
  • 38
2

I had faced same problem!! Just update your build.gradle file to resolve above issue.

classpath 'com.android.tools.build:gradle:3.0.1'

compileSdkVersion 26

buildToolsVersion '26.0.3'

Update your AndroidStudio to latest vetsion.

Enjoy your day.

1

Android Studio 2.4 includes support for all the new developer features available with Android O you need to update studio

And create XML file

1) Right-click the font folder and go to New > Font resource file. The New Resource File window appears.

2) Enter the file name, and then click OK. The new font resource XML opens in the editor.

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
    android:fontStyle="normal"
    android:fontWeight="400"
    android:font="@font/Montserrat_Regular.otf" />

</font-family>
akhilesh0707
  • 6,709
  • 5
  • 44
  • 51
0

This is kind of generic issue and I faced this one with error message

main\res\lib\poi-3.12-android-a.jar: Error: The file name must end with .xml

I had placed this jar file inside resource folder(res), I removed this jar file and folder it get resolved. Avoid any unwanted files/folder inside res directory.

user3004692
  • 23
  • 1
  • 4