Home » Android » Font in XML with Android O

Font in XML with Android O

Posted by: admin June 15, 2020 Leave a comment

Questions:

Android O give font directory with new android-studio version 3.0 but when in drop font-file in font directory and run project it give me a error in font file in font directory.
Error:Execution failed for task ‘:app:mergeDebugResources’.

/home/ttuser4/Downloads/fontPractice/app/src/main/res/font/Dancing_Script.ttf: Error: The file name must end with .xml

How to&Answers:

I also got the similar issue. To fix that i followed the below steps.

  1. Updated Android Studio to latest canary build.
  2. Change distributionUrl in project-dir/gradle/gradle-wrapper.properties to latest gradle distribution url(You can get the latest availability of gradle from here) to update the gradle version used to build like below.

    distributionUrl=https://services.gradle.org/distributions/gradle-4.1-milestone-1-all.zip

  3. Then update com.android.tools.build:gradle version in the root build.gradle to the latest like below. (Note: Below mentioned version is the latest one when the answer is posted. The version might be change in future. So take care about that.)
buildscript {
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-alpha4'
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com'
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
  1. Sync, Clean and update the project.

It worked for me. Let me know with your comments.

Answer:

I am assuming you are using an older build tools plugin, so it does not know about res/font and the new font capabilities.

I had the same problem I update to gradle build tools 2.4.0-alpha7

but I got an error message on build and then had to set environment variable

launchctl setenv ANDROID_DAILY_OVERRIDE <your-value-on-error-message>

Answer:

Suppose you have opened your Android Studio Project.

  1. Right click on res directory, and select New > Android Resource Directory.

  2. Select resource directory “font”.

enter image description here

  1. Change your font name to “dancing.ttf” (capitalized names generate error). I used “Pacifico-Font” and got the following error message –

Error:Execution failed for task ‘:app:mergeDebugResources’.
res/font/Pacifico-Font.ttf: Error: ‘P’ is not a valid file-based resource name character: File-based resource names must contain only
lowercase a-z, 0-9, or underscore

When I changed it to pacifico.ttf, it worked.

  1. Add the font to the fonts folder.
  2. Run your app.

I’ve followed the exact same steps on the Android Studio 3.0 Preview with “pacifico.ttf” font, and it works for me.

enter image description here

In case you encounter any errors, clean project once and then run it again.

Do let me know if you still run into issues.

Answer:

You can add font in any format, but you have to create a Font Family xml file in order to apply these fonts in xml layout file. A font family is an XML file that contains multiple font files along with its style and weight details. You can access the font family as a single unit.

To create a font family, perform the following steps in the Android Studio:

  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.

  3. Enclose each font file, style, and weight attribute in the element. The following XML illustrates adding font-related attributes in the font resource XML:

    <?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/lobster_regular" />
        <font
            android:fontStyle="italic"
            android:fontWeight="400"
            android:font="@font/lobster_italic" />
    </font-family>
    

Then you can add this font-family to any element in layout containing text. Like in this example its applying to TextView,

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/lobster"/>