B4A Library FontAwesome

NJDude

Expert
Licensed User
This library will allow you to use FontAwesome on any view that supports Typeface (except Spinner).

Requirements:

1- B4A 4.x.

2- Copy the FontAwesome.xml and FontAwesome.jar to your additional libraries directory.

Usage:
B4X:
'Initialize the library.
Private FA As FontAwesome

FA.Initialize

'Add the FontAwesome typeface to your view.
Label1.Typeface = FA.FontAwesomeTypeface

'Select the icon you want to display.
Label1.Text = FA.GetFontAwesomeIconByName("fa-android") & "  Some title label  " & FA.GetFontAwesomeIconByNumber(0xf15d)
That's it!!

Notes:

* If an icon isn't defined the library will show a "?" or a blank-square (get all the defined icons by name and their numbers from the Cheat Sheet)

Screenshot:

 

Attachments

Last edited:

ivan.tellez

Active Member
Licensed User
I dont see the real advantage of having a library, its actually more code.



B4X:
'Initialize the Typeface.
Dim myFont As Typeface
myFont = Typeface.LoadFromAssets("fontawesome.otf")

'Add the FontAwesome typeface to your view.
Label1.Typeface = TheTypeFace

'Select the icon you want to display
Label1.Text = Chr(61481)
 

thedesolatesoul

Expert
Licensed User
I'll list some of the advantages:
- The font itself is embedded in the library, so the extra step of adding (and later removing) it to your assets is not required. (quite handy when quick prototyping).
- You can mention font charachter by their name to make the code more meaningful and easier to remember and identify.
- The library has another convenience method called SetFontAwesomeTypefaceByTag, this allows you to recursively set all views to this typeface based on a tag (or even a null tag will set all items to that typeface).

Arguably it is only one more line of code due to FA.Initialize which is unavoidable. But in the longer sense i feel its more optimized if you only load the typeface once.
For e.g. the above 4 lines you posted are equivalent to these 4 lines:
B4X:
   Private FA As FontAwesome
   FA.Initialize
   Label1.Typeface = FA.FontAwesomeTypeface
   Label1.Text = FA.GetFontAwesomeIconByNumber(61481)
You could argue that your above code can be further reduced by 2 lines, but assigning the font multiple times will require multiple disk accesses if you do that kind of code reduction.
I'm sure you will see the benefit both in number of code lines and loading the typeface when you have much more views to deal with.
 

NJDude

Expert
Licensed User
- The library has another convenience method called SetFontAwesomeTypefaceByTag, this allows you to recursively set all views to this typeface based on a tag (or even a null tag will set all items to that typeface).
Note that there's also a method to add FontAwesome to all the views in one shot:
B4X:
FA.SetFontAwesomeTypefaceToAllViews(Activity)
 

Alberto Michelis

Well-Known Member
Licensed User
Can it be uset in Activity.AddMenuItem ?

I try with:

Activity.AddMenuItem(FA.GetFontAwesomeIconByName("fa-money")&" Tenencia","mTenencia")

and did not work

Thanks
 

Dave O

Active Member
Licensed User
If you're using the PopupMenu component of the stdActionBarHelper library (which is how I add multiple menus to my apps), you can add icons like this:
B4X:
Dim overflowMenu As PopupMenu
overflowMenu.Initialize("overflowMenu", overflowButton)
Dim bmd As BitmapDrawable
bmd.Initialize(LoadBitmap(File.DirAssets, "share32.png"))
overflowMenu.AddMenuItem(common.SHARE_PROTO, "Share as HTML", bmd)
 

jotaele

Member
Licensed User
Is posible to use two fonts in a Label? I think that no but...

When you use FA to put an icon, the rest of the font of the label changes and it's different to the font of the app.

Thanks
 

Inman

Well-Known Member
Licensed User
This is awesome. You can display icons near text, without worrying about adding an imageview and then positioning, padding and aligning it correctly with the text.

Thanks man.
 

jotaele

Member
Licensed User
Thanks. I will use this workarround.

'btnMapa
btnMapaIcon.Initialize("btnMapa")
btnMapa.Initialize("btnMapa")
btnMapaIcon.Typeface = FA.FontAwesomeTypeface
btnMapaIcon.Color = Colors.RGB(62, 99, 143)
btnMapa.Color = Colors.RGB(62, 99, 143)
btnMapaIcon.TextSize = 24
btnMapa.TextSize = 16
btnMapaIcon.TextColor = Colors.White
btnMapa.TextColor = Colors.White
btnMapaIcon.Text = FA.GetFontAwesomeIconByName("fa-globe")
btnMapa.Text = "Mapa"
Activity.AddView(btnMapa, 30dip, 70dip, 100%x - 60dip, 70dip)
Activity.AddView(btnMapaIcon, 30dip, 70dip, 60dip, 70dip)
 

jotaele

Member
Licensed User
This library will allow you to use FontAwesome on any view that supports Typeface (except Spinner).

Requirements:

1- B4A 4.x.

2- Copy the FontAwesome.xml and FontAwesome.jar to your additional libraries directory.
It could be interesting to put a generic method to change the font for another symbolic font, even the method FA.GetFontAwesomeIconByName("fa-XXX") don't work.

It would be amazing.

It's only a wish-suggestion. :)

Another wish could be to set the typeface that the generic font (not the font of the symbols) to get the Roboto font, for example.

Thanks
 

Enrique Gonzalez R

Well-Known Member
Licensed User
Hello Guys,

I quite enjoy this library but find frustrating many things, for example: is in Hex, No Grouping, do not know if they are all continuous, bla bla bla... of course non are library specific problem.

so i made a file that help me with that, and wanted to share.
 

Attachments

Jaames

Active Member
Licensed User
Hello Guys,

I quite enjoy this library but find frustrating many things, for example: is in Hex, No Grouping, do not know if they are all continuous, bla bla bla... of course non are library specific problem.

so i made a file that help me with that, and wanted to share.
And example Icons of all fonts, their names and hex values in listview (copied from the cheatsheet).
 

Attachments

Top