How to set custom font in xml Android

How to set custom font in xml Android - Hello friend inabnomaniiyaha, In the article that you read this time with the title How to set custom font in xml Android, we have prepared this article well for you to read and take information in it. hopefully the contents of the post Artikel android-example,what we write you can understand. Alright, happy reading.

Judul : How to set custom font in xml Android
link : How to set custom font in xml Android

Baca juga


How to set custom font in xml Android

Android API 16+

Beginning with Android 8.0 (API 26), there is native support for setting the fonts in XML. However, using the support library extends this down to Android 4.1 (API 16).

1. Add a font to your project

Right click the res folder and go to New > Android Resource Directory. Type font as the name and font as the Resource type.

Copy and paste your font into the new res/font directory. I'm just using a single font in my example, the regular dancing script font. I renamed it to dancing_script.ttf just to avoid any potential naming problems with upper case or illegal characters.

How to set custom font in xml Android


2. Create a font-family XML file.

Right click the res/font folder and choose New > Font Resource File. Call it whatever you want. I'm calling mine my_custom_font.xml.

Paste in the following code. Note that the attributes are set twice, once for android (API 26+) and once for app (API 16+).

<?xml version="1.0" encoding="utf-8"?>

<font-family xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto">

    <font

        android:fontStyle="normal"

        android:fontWeight="400"

        android:font="@font/dancing_script"

        app:fontStyle="normal"

        app:fontWeight="400"

        app:font="@font/dancing_script"

        />

</font-family>

3. Set the font in XML

Now you can just use the fontFamily attribute to set the font in XML.

<TextView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="Hello World!"

    android:fontFamily="@font/my_custom_font" />

Notes

Read the documentation for more help.

If you need to support pre-API 16, then just set the font programmatically.



That's the articleHow to set custom font in xml Android

That's it for the article How to set custom font in xml Android this time, hopefully can be useful for all of you. well, see you in another article post.

You are now reading the articleHow to set custom font in xml Android with link addresshttps://inabnonapudyawanabing.blogspot.com/2021/01/how-to-set-custom-font-in-xml-android.html

0 Response to "How to set custom font in xml Android"

Post a Comment

Tips Tricks for Android Phone

Tips & Tricks for Android Phone is a free android app and Collection of Tips and Tricks related to using your android mobile device lik...