Android Tutorial Setting custom font for whole application without any library

Hi everyone,

After a week I have finally figured it out how to add a custom font for the whole application without any library or extra code.

Here is the solution:
  1. Create a folder resource inside your project folder.
    upload_2018-12-1_13-1-31.png


  2. Create a new folder font inside the resource folder and copy your font file inside it.
  3. The font file name should not contain any characters except _, a-z and 0-9
  4. Open Manifest Editor and paste this following code
    B4X:
    CreateResource(font, custom_font.xml,
    <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/custom_font_file_name_without_extension"/>
        <font android:fontStyle="italic" android:fontWeight="400" android:font="@font/custom_font_file_name_without_extension" />
        <font app:fontStyle="normal" app:fontWeight="400" app:font="@font/custom_font_file_name_without_extension"/>
        <font app:fontStyle="italic" app:fontWeight="400" app:font="@font/custom_font_file_name_without_extension" />
    </font-family>
    )
    For API level 26 or lower use the app namespace to ensure your fonts load.

  5. Then add this following code to apply custom font
    B4X:
    CreateResource(values, theme.xml,
    <resources>
        <style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <item name="android:fontFamily">@font/CustomFontStyle</item>
            <item name="fontFamily">@font/CustomFontStyle</item>
        </style>
    </resources>
    )
    SetApplicationAttribute(android:theme, "@style/MyTheme")
    Change Theme.AppCompat.Light.NoActionBar as per you requirement.

  6. Add the resource folder in your main activity like this
    B4X:
    #AdditionalRes: "..\resource"

  7. That's it!
To use bold typeface check the following code
B4X:
Dim lb As Label
lb.Initialize("")
lb.Typeface = Typeface.CreateNew(lb.Typeface,Typeface.STYLE_BOLD)

UPDATE: I have updated this tutorial for API level 26 and lower (upto API level 16). Now you can use a custom font on devices running Android 4.1 and higher. To use the Fonts in XML feature on devices running Android 4.1 (API level 16) and higher, use the Support Library 26 or higher. Remember to use the app namespace to ensure your fonts load on API level 26 to 16.
 
Last edited:

hatzisn

Expert
Licensed User
Longtime User
What type of font file is accepted? What extensions does the file of the font must have? ttf or something else?
 

Brandsum

Well-Known Member
Licensed User
Android docs doesn't specify the fonts that it supports.When Android dislikes a custom font, rather than raising an Exception, it seems to substitute Droid Sans ("sans")

I will recommend to use google font. I took some fonts for my projects and all of the fonts are woking fine.
 

hatzisn

Expert
Licensed User
Longtime User
Thank you Brandsum for the info...
 

Sinan Tuzcu

Well-Known Member
Licensed User
Longtime User
Get error

CreateResource(font, custom_font.xml,
<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/opensans_regular"/>
<font android:fontStyle="italic" android:fontWeight="400" android:font="@font/opensans_regular" />
<font app:fontStyle="normal" app:fontWeight="400" app:font="@font/opensans_regular"/>
<font app:fontStyle="italic" app:fontWeight="400" app:font="@font/opensans_regular" />
</font-family>
)

CreateResource(values-v21, theme.xml,
<resources>
<style name="LightTheme" parent="@android:style/Theme.Material.Light">
<item name="android:colorAccent">#FF5E7A0B</item> <!-- checkboxes,, switches,, etc. -->
<item name="android:fontFamily">@font/opensans_regular</item>
<item name="fontFamily">@font/opensans_regular</item>
</style>
</resources>
)

SetApplicationAttribute(android:theme, "@style/MyTheme")

upload_2019-3-23_6-45-49.png
 

DonManfred

Expert
Licensed User
Longtime User
Get error

CreateResource(font, custom_font.xml,
<
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/custom_font_file_name_without_extension"/>
<
font android:fontStyle="italic" android:fontWeight="400" android:font="@font/custom_font_file_name_without_extension" />
<
font app:fontStyle="normal" app:fontWeight="400" app:font="@font/custom_font_file_name_without_extension"/>
<
font app:fontStyle="italic" app:fontWeight="400" app:font="@font/custom_font_file_name_without_extension" />
</
font-family>
)

Post the relevant Manifest code. Seems you are not using it correctly?

Away from that you REALLY should create a NEW THREAD for any question/issue you have. Posting to a tutorial thread is the wrong way to ask for help
 
Top