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:
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.
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:
- Create a folder resource inside your project folder.
- Create a new folder font inside the resource folder and copy your font file inside it.
- The font file name should not contain any characters except _, a-z and 0-9
- 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> )
- 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")
- Add the resource folder in your main activity like this
B4X:#AdditionalRes: "..\resource"
- That's it!
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: