Today, I bring you another simple tutorial on how to add custom fonts easily to B4A android applications.
B4A is the fastest growing android applications development IDE out there. With a large community of developers always available to attend to your needs.
A lot of people emailed me asking this questions. Now, why would you want to add custom fonts to your applications? N0te I will be using font and typeface interchangeably. Lets get stared.
WHY ADD CUSTOM FONTS TO YOUR ANDROID APPLICATIONS
Well, applications are not only about coding, the interface also matters. B4A currently has 3 typefaces that is SERIF, Monospace and Sans-Serif which is not enough. And typefaces like handwriting font or even your official corperate typeface can also make your UI beautiful.
Ways in Adding typeface
I will be highlighting on 3 ways but we will actually use two method.
You can add the typeface by Code.
To do this;
- click on File Manager in your IDE
- click on Add Files
- Navigate to where you have your fonts on your computer and add them
- Next Write this few lines of code to apply it to labels, edittext, or any other control you like.
'Declare font variables
Dim BerckerBlack As Typeface
'Declare variable to hold second typeface
Dim LatoRegular As Typeface
'Declare the variable to hold the third typeface
Dim Smoothie As Typeface
'Assign fonts to variables
BerckerBlack = Typeface.LoadFromAssets("BeckerBlackNF.ttf")
LatoRegular = Typeface.LoadFromAssets("Lato-Regular.ttf")
Smoothie = Typeface.LoadFromAssets ("Smoothie Shoppe.ttf")
'Apply fonts (typeface) to a view
lblOne.Typeface = BerckerBlack
lblOther.Typeface = LatoRegular
lblSmo.Typeface = Smoothie
Looking a the Code Above, I added 3 files to my assets folder, i then declared a variable with type “TYPEFACE”
I loaded the fonts and assign it tot he variables.
Now the last thing left is just applying it to the controls. If you want to apply the typeface to all views at once, you can use this code below
'==APPLY TO ALL LABELS, EDITTEXTS, BUTTONS, CHECKBOXES, RADIOBUTTONS AND TOGGLEBUTTONS
For Each v As View In Activity.GetAllViewsRecursive
If v Is Label Then
Dim lbl As Label = v
lbl.Typeface = LatoRegular
Adding the typeface through the Wysiwyg Designer
To do this
- Click to open the WYSIWYG Designer to open
- click on add files – A dialog will open, choose the font you will like to include
- Select the View you will like to apply the font to
- click on the Typeface dropdown in the properties pane, your new fonts should be listed there
- Select it and apply. See image below
Finally the third method
Using a library
There is a library by NJDude in the b4a Forum. Download and add to your project.
Instructions are on this PAGE HERE
DOWNLOAD SOURCE CODE [wpdm_package id=’5950′]
Congratulations. Remember to leave your comments below if you find any difficulties.