Android Code Snippet Using Material/FontAwesome Icons as Bitmaps

Kwame Twum

Active Member
Licensed User
This sub will let you use icon fonts as bitmaps

B4X:
Sub FontBit (icon As String, font_size As Float, color As Int, awesome As Boolean) As Bitmap
    If color = 0 Then color = Colors.White
    Dim typ As Typeface = Typeface.MATERIALICONS
    If awesome Then typ = Typeface.FONTAWESOME
    Dim bmp As Bitmap
    bmp.InitializeMutable(32dip, 32dip)
    Dim cvs As Canvas
    cvs.Initialize2(bmp)
    Dim h As Double
    If Not(awesome) Then
        h = cvs.MeasureStringHeight(icon, typ, font_size) + 10dip
    Else
        h = cvs.MeasureStringHeight(icon, typ, font_size)
    End If
    cvs.DrawText(icon, bmp.Width / 2, bmp.Height / 2 + h / 2, typ, font_size, color, "CENTER")
    Return bmp
End Sub
Usage example:
B4X:
Activity.AddMenuItem3("Search", "search", FontBit(Chr(0xE8B6), 25, Colors.Black, False), True)
 
Last edited:
Top