Android Code Snippet Combining a Fontawesome Icon And Text on a View Using Richstring

Description:
Erel’s recent code snippet regarding Material Icons and Richtsring inspired me to write the following snippet combining a FontAwesome Icon, text and Richstring to put text on a label, button, edittext, etc. The following code which can be enhanced, is the entire project:

B4X:
Sub Globals
    Private Label1 As Label
    Private lblFontAwesomeIcon As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.loadlayout("1")  'layout has 2 labels: Label1 and lblFontAwesomeIcon
    Label1.Typeface = lblFontAwesomeIcon.Typeface   'lblFontAwesomeIcon must have fontawesome Typeface. Can be invisible.
    Label1.text = FontAwesomeIconsWithRichString("0xF0B2", "B4A Takes You Anywhere!", 2.75)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then Activity.Finish
End Sub

Sub FontAwesomeIconsWithRichString(HexCode As String, Text As String, IconRelativeSize As Float) As RichString
    Dim rs As RichString
    HexCode=HexCode.Replace("0x","")
    Dim intHC As Int = Bit.ParseInt(HexCode, 16)
    rs.Initialize($"{SIZE2}{C}${Chr(intHC)}  {C}{SIZE2}{B}${Text}{B}"$)
    rs.RelativeSize2(IconRelativeSize,"{SIZE2}")
    rs.Color2(Colors.Cyan,"{C}")
    rs.Style2(rs.STYLE_BOLD_ITALIC,"{B}")
    Return rs
End Sub
Tags: Richstring, Fontawesome
Dependency: Richstring Library (that’s all)
 

Attachments

  • FontAwesomeRichstring.png
    FontAwesomeRichstring.png
    16.7 KB · Views: 872

Mahares

Expert
Licensed User
Longtime User
I'm not sure why this code is needed

Actually, this snippet is part of a larger project where I create a SQLite database with 4 columns: fontCode, FontawesomeICon, Description and IconCategory. The data from 776 records is then displayed on a listview, where the item click of any record is automatically transferred to the textview in question:
txt="CREATE TABLE IF NOT EXISTS " & DBTableName & " (HEX_CODE TEXT, FONT_NAME TEXT, CATEGORY TEXT,HEX_ICON BLOB, " _
& " PRIMARY KEY (HEX_CODE, FONT_NAME))"
The data is imported from a text file into the SQLite table where the HEX_CODE is converted to HEX_ICON.
B4X:
Sub MyLV_ItemClick (Position As Int, Value As Object)   'MyLV is a listview
    Dim strArray() As String = Regex.Split(",",Value)
    btnFA.text = FontAwesomeIconsWithRichString(strArray(0), strArray(1) & TAB & TAB & strArray(2), 2.75)
End Sub
See picture below for the listview layout and item click result.
 

Attachments

  • FontAwesomeDatabase.png
    FontAwesomeDatabase.png
    85.8 KB · Views: 831

Mahares

Expert
Licensed User
Longtime User
Read it too fast.. :confused: (thought it was Material Icon combined with a text font)
Sorry for wasting your 'Like'. I owe you a 'Like' the next time you publish a code snippet. If you read my post#4, you will see why I created it. After all, that is why it is called a code snippet. It does not tell the whole story. There is no icon picker involved here. It is all via code.
 
Top