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

Discussion in 'Code Snippets' started by Mahares, Feb 10, 2017.

  1. Mahares

    Mahares Well Known Member Licensed User

    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:

    Code:
    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)
     

    Attached Files:

    stanks and inakigarm like this.
  2. Erel

    Erel Administrator Staff Member Licensed User

    I'm not sure why this code is needed. You can use FontAwesome directly:
    Code:
    Label1.Text = Chr(0xF0B2) & " B4A Takes You Anywhere!" 'Chr(0xF0B2) is taken directly from the icon picker tool
    You can of course use RichString to further modify the text appearance.
     
  3. Mahares

    Mahares Well Known Member Licensed User

    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.
    Code:
    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.
     

    Attached Files:

  4. Mahares

    Mahares Well Known Member Licensed User

    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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice