Search code examples
androidxmlandroid-studiofontscustom-font

The new Custom Font Method in android using xml


As android released its O preview, a new feature has been added called fonts in xml. Its easy to implement but i have few doubts.

let me first add the best way to do it
1.)Right-click the res folder and go to New > Android resource directory. The New
Resource Directory window appears.
2.)In the Resource type list, select font, and then click OK.
3.)Add your font files in the font folder.The folder structure below generates R.font.dancing_script, R.font.la_la, and R.font.ba_ba.
4.)Double-click a font file to preview the file's fonts in the editor.

Next we must create a font family

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 font tag 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/hey_regular" />
    <font
    android:fontStyle="italic"
    android:fontWeight="400"
    android:font="@font/hey_bababa" />
</font-family>

Adding fonts to a TextView:

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

As from the documentation all the steps are correct.
my question is:
1.)Can this work in android lollipop or marshmallow
2.)Is any support library needed for implementing the features
3.)What type of font extension will this support like .ttf .otf



Please give me the best explanation.


Solution

  • 1.)Can this work in android lollipop or marshmallow

    Ans : It's work for lower Version also(Based on my research).

    2.)Is any support library needed for implementing the features

    Ans : No need support Lib only you need to update your sdk(Android O)

    3.)What type of font extension will this support like .ttf .otf

    Ans: Yes, Its support .ttf, .otf font Files.

    For more info you can see below links :

    Android Doc for Font Family Api , Android-O Preview Video