Android Question MaterialIcons-WebFont misterious behavior

giggetto71

Active Member
Licensed User
Longtime User
Hi,
I am using from many years in my published app the MaterialIcons-WebFont.txt (attached) which is added to the app files and then loaded in an internal map (key: name, object: icon) using this simple function:
B4X:
Sub LoadData
    NameToChar.Initialize
    For Each line As String In File.ReadList(File.DirAssets, "MaterialIcons-WebFont.txt")
       NameToChar.Put(line.SubString(1), line.CharAt(0))
    Next
End Sub

after that a listview is filled with

B4X:
Sub UpdateList(init As Boolean)
  
    Dim ii As Int
    lstIcons.Clear
    If init Then
       For ii  = 0 To NameToChar.Size-1
          lstIcons.AddSingleLine(NameToChar.Get(ii))     
       Next
    Else
       For ii  = 0 To FoundList.Size-1
          lstIcons.AddSingleLine(NameToChar.Get(FoundList.Get(ii)))     
       Next     
    End If
  
End Sub
the Listview is configured in the activity create as:

Code:
lstIcons.SingleLineLayout.Label.TextSize = 30
    lstIcons.SingleLineLayout.ItemHeight = 45dip
    lstIcons.SingleLineLayout.Label.Typeface = Typeface.LoadFromAssets("materialdesignicons-webfont.ttf")

so really really simple and straightforward.

In all my phones and also in the past 4 years it worked just fine and the result (the picture just shows the first 4 icons and the first one name (the key):
1740677588939.png


now, one user sees this:

1740677710763.png




any idea? what could it be?
 

Attachments

  • materialicons-webfont.txt
    60.4 KB · Views: 102
Last edited:

MicroDrie

Well-Known Member
Licensed User
Longtime User
Based on the text file, it appears that it has a three-digit UTF-8 format. Somewhere in your program, you will convert this to the two-digit UTF-16 format and, for example, print it out with a Chr(0xf521) command as the graphical representation of the first toggle switch. In the B4J [B4X] MaterialIcons Web Font Chooser example by Erel, this conversion step is skipped by using UTF-16 code. The characters are different, so a font is used. Whether your conversion is successful or whether the wrong font is being used cannot be determined from the summary information.

If the 719 icons are enough for you, you could use the font and associated text file from Erel's example to avoid the conversion from UTF-8 to UTF-16 and see if the problem is solved or you can use maybe directly the chr() code.
 
Upvote 0

giggetto71

Active Member
Licensed User
Longtime User
@MicroDrie thanks for your reply. I am not a font expert so I am not sure I got completely the point. both the font and the text file are exactly the ones taken from Erel's example and actually the LoadData function is taken from that example as it is.
as for using the chr(), I use the CharAt calculated in the LoadData which I believe is the same thing, but I may be wrong.

the only way that I found to reproduce what the user is seeing is commenting the line where I load the font for the listview in the Activity_create. So if I do:

B4X:
lstIcons.SingleLineLayout.Label.TextSize = 30
lstIcons.SingleLineLayout.ItemHeight = 45dip
'lstIcons.SingleLineLayout.Label.Typeface = Typeface.LoadFromAssets("materialdesignicons-webfont.ttf")

the SingleLineLayout.Label object does not load the webfont font and it shows:

1740821574929.png


which to me makes sense as the SingleLineLayout.Label probably uses at that point its default font which of course is not the webfont.
This makes me think that

B4X:
Typeface.LoadFromAssets("materialdesignicons-webfont.ttf")

for some reasons fails, but i know that the app did not crash as the user did not complain and I did not see any crash on the google development console.
any more ideas?
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
Working B4J example which give described error (left picture) without lines 17, 18, 21 and 23.
Webfont example:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
  
    Private fnt As B4XFont
    Private lbl1 As Label
    Private lbl2 As Label
    Private lbl3 As Label
    Private lbl4 As Label
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    'load the layout to Root
    Root.LoadLayout("frmWebFont")
    fnt = xui.CreateFont(LoadWebFont,26)
    lbl1.Font = fnt
    lbl1.Text = $"${Chr(0xf521)} toggle-switch"$
    lbl2.Font = fnt
    lbl2.Text = $"${Chr(0xf522)} toggle-switch-off"$
    lbl3.Font = fnt
    lbl3.Text = $"${Chr(0xfa18)} toggle-off-outline"$
    lbl4.Font = fnt
    lbl4.Text = $"${Chr(0xfa19)} toggle-switch-outline"$
End Sub

Private Sub LoadWebFont As Object
    #if B4J
    Dim fx As JFX
    Return fx.LoadFont(File.DirAssets, "materialdesignicons-webfont.ttf", 30)
    #else if B4A
    Return Typeface.LoadFromAssets("materialdesignicons-webfont.ttf")
    #else if B4i
'    For Each f As String In Font.AvailableNames
'        LogPrt(f)
'    Next
    Return Font.CreateNew2("Material-Design-Icons", 32)
    #End If
End Sub

1740836466476.png


update: Given source code in this post is for B4J. Attached fontTest.bas file is for both B4J and B4A.
 

Attachments

  • B4XFontTest.bas
    1.7 KB · Views: 88
Last edited:
Upvote 0
Top