Android Question Changing the system font shows Material Icons label with Chinese-like chars

Alessandro71

Well-Known Member
Licensed User
Longtime User
I need to create a label by code, by cloning an existing label, which uses Material Icons font.
Here is how it looks: the grey icon is the original label, while the red one is the clone
1726865196306.png


if I set the system font to anything else than the default (SamsungOne in this example)
1726865257317.png

the cloned icon font changes to a strange font
1726865295379.png


proof-of-concept project is attached.
 

Attachments

  • TestFont.zip
    9.8 KB · Views: 44

TILogistic

Expert
Licensed User
Longtime User
Test Clone

B4X:
    Dim new As B4XView = CloneLabelToB4XView(Label1)
    Log(new.Font)
    Log(new.Text)
B4X:
Public Sub CloneLabelToB4XView(lbl As Label) As B4XView
    Dim new As B4XView = lbl
    new.Text = lbl.Text
    new.Font = xui.CreateFont(lbl.Font, lbl.Font.Size)
    new.TextColor = xui.Color_Red
    Return new
End Sub
1726868801431.png
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
Test Clone

B4X:
    Dim new As B4XView = CloneLabelToB4XView(Label1)
    Log(new.Font)
    Log(new.Text)
B4X:
Public Sub CloneLabelToB4XView(lbl As Label) As B4XView
    Dim new As B4XView = lbl
    new.Text = lbl.Text
    new.Font = xui.CreateFont(lbl.Font, lbl.Font.Size)
    new.TextColor = xui.Color_Red
    Return new
End Sub
View attachment 157147

it does not work in B4A, since Label does not have a Font member (and that's why I'm using B4XView in my code):
1726906482319.png
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
I've added some logging to my code

B4X:
    Log(Label1.Font.ToNativeFont)
    Log(newlabel.Font.ToNativeFont)

and both the source label and the clone one appear to use the same font, but still looks different on screen

Rich (BB code):
(Typeface) android.graphics.Typeface@e7a0787e
(Typeface) android.graphics.Typeface@e7a0787e
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Try adding the code to see what is shown.
B4X:
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Typeface=Typeface.MATERIALICONS
    lbl.Text=Chr(0xE92B)
    lbl.TextSize=28
    lbl.TextColor=xui.Color_Blue
    Root.AddView(lbl, 100dip, 150dip, 50dip, 50dip)
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
Try adding the code to see what is shown.
B4X:
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Typeface=Typeface.MATERIALICONS
    lbl.Text=Chr(0xE92B)
    lbl.TextSize=28
    lbl.TextColor=xui.Color_Blue
    Root.AddView(lbl, 100dip, 150dip, 50dip, 50dip)

manually setting the typeface works, but the big picture requires cloning a label whose font is not always MATERIALICONS
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Try cloning label, define label1 and newlabel as label.
B4X:
newlabel.Typeface=Label1.Typeface
...'text size color etc
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
modified as following
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    
    nativeLabel.Initialize("")
    nativeLabel.Typeface = Label1.As(Label).Typeface

    newlabel = nativeLabel
    newlabel.Text = Label1.Text
    newlabel.TextSize = Label1.TextSize
    newlabel.TextColor = xui.Color_Red
    
    Root.AddView(newlabel, 100dip, 100dip, 50dip, 50dip)
End Sub

but still different font
1726927443856.png
 
Upvote 0

teddybear

Well-Known Member
Licensed User
I mean you define label1 as label instead casting it to Label
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
All converted to Label

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Label1 As Label
    
    Private nativeLabel As Label
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    
    nativeLabel.Initialize("")
    nativeLabel.Typeface = Label1.Typeface
    nativeLabel.Text = Label1.Text
    nativeLabel.TextSize = Label1.TextSize
    nativeLabel.TextColor = xui.Color_Red

    Root.AddView(nativeLabel, 100dip, 100dip, 50dip, 50dip)
End Sub

still wrong font
1726941349354.png
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
test
I still don't understand what you want to do.
Here is a simple example of cloning label1 from the design to another from the design and another by programming.
B4X:
    Dim TextFont1 As B4XFont = xui.CreateMaterialIcons(48)
 
    Label1.Text = Chr(0xE92B)
    Label1.Font = TextFont1
    Label1.TextColor = xui.Color_Blue
 
    Label2.Text = Label1.Text
    Label2.Font = TextFont1
    Label2.TextColor = Label1.TextColor
 
    Dim lbl As Label
    lbl.Initialize("")
    Dim CloneLabel As B4XView = lbl
    CloneLabel.text = Label1.Text
    CloneLabel.Font = TextFont1
    CloneLabel.TextColor = Label1.TextColor
 
    Root.AddView(CloneLabel, 200dip, 200dip, 50dip, 50dip)

View attachment 157187

I had to download the sources to simulate your error.

use:
B4X:
Public Sub GetFontCustom(FontName As String, FontSize As Int) As B4XFont
    'B4i Add to main
    '#AppFont: <Font name>.ttf
    Dim FontResult As B4XFont
    #If B4A
    FontResult = xui.CreateFont(Typeface.LoadFromAssets(FontName),FontSize)
    #Else If B4J
    Dim fx As JFX
    FontResult = xui.CreateFont(fx.LoadFont(File.DirAssets, FontName, FontSize), FontSize)
    #Else
    FontResult = xui.CreateFont(Font.CreateNew2(FontName, FontSize), FontSize)
    #End If
    Return FontResult
End Sub
explicitly setting the font works fine, but my use case is more general: I'm writing a library that does not know beforehand which font is used for each label.
It may be Material or FontAwesome or anything else: thus the need to clone existing label settings.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
test b4a, b4j
use library XUI Views ( XUIViewsUtils.CreateLabel)
B4X:
    Label1.Text = Chr(0xE92B)
    Label1.Font = xui.CreateMaterialIcons(48)
    Label1.TextColor = xui.Color_Blue
    
    Label2.Text = Chr(0xF014)
    Label2.Font = xui.CreateFontAwesome(48)
    Label2.TextColor = Label1.TextColor
    
    Root.AddView(CloneLabel(Label1), 200dip, 170dip, 50dip, 50dip)
    Root.AddView(CloneLabel(Label2), 200dip, 250dip, 50dip, 50dip)
B4X:
Public Sub CloneLabel(TargetLabel As B4XView) As B4XView
    Dim OutLabel As B4XView = XUIViewsUtils.CreateLabel
    OutLabel.Font = xui.CreateFont2(TargetLabel.Font, TargetLabel.Font.Size)
    OutLabel.TextColor = TargetLabel.TextColor
    OutLabel.Text = TargetLabel.Text
    Return OutLabel
End Sub
or
B4X:
'Create a Label.
Public Sub CreateLabel As B4XView
    Dim lbl As Label
    lbl.Initialize("")
    Return lbl
End Sub
1726998837500.png
 
Last edited:
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
test b4a, b4j
use library XUI Views ( XUIViewsUtils.CreateLabel)
B4X:
Root.AddView(CloneLabel(Label1), 200dip, 200dip, 50dip, 50dip)
B4X:
Public Sub CloneLabel(TargetLabel As B4XView) As B4XView
    Dim OutLabel As B4XView = XUIViewsUtils.CreateLabel
    OutLabel.Font = xui.CreateFont2(TargetLabel.Font, TargetLabel.Font.Size)
    OutLabel.TextColor = TargetLabel.TextColor
    OutLabel.Text = TargetLabel.Text
    Return OutLabel
End Sub

View attachment 157197
or

B4X:
'Create a Label.
Public Sub CreateLabel As B4XView
    Dim lbl As Label
    lbl.Initialize("")
    Return lbl
End Sub
thank you for your continued effort, I was also experimenting with a code quite like yours, but still have different results on my samsung phone.
I added a couple of labels and I see that the issue font happens only with Material and Awesome.
The default font is unaffected
updated proof-of-concept code attached: this happens only when a font other than default is selected in the screen options of the samsung/android control panel

1726998678533.png
 

Attachments

  • TestFont.zip
    10 KB · Views: 38
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
see log please

What do the logs tell you?
B4X:
Private Sub CloneLabel(source As B4XView) As B4XView
    Dim newlabel1 As B4XView = XUIViewsUtils.CreateLabel
    newlabel1.Font = xui.CreateFont2(source.Font, source.Font.Size)
    newlabel1.Text = source.Text
    newlabel1.TextSize = source.TextSize
    newlabel1.TextColor = source.TextColor

    Log(source.Font)
    Log(newlabel1.Font)

    Return newlabel1
End Sub
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
see log please

What do the logs tell you?
B4X:
Private Sub CloneLabel(source As B4XView) As B4XView
    Dim newlabel1 As B4XView = XUIViewsUtils.CreateLabel
    newlabel1.Font = xui.CreateFont2(source.Font, source.Font.Size)
    newlabel1.Text = source.Text
    newlabel1.TextSize = source.TextSize
    newlabel1.TextColor = source.TextColor

    Log(source.Font)
    Log(newlabel1.Font)

    Return newlabel1
End Sub


my sample with your above code:

1727001124629.png



logs
1727001214139.png
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
I can reproduce the problem.
What is not obvious in this thread is that the OP is talking about using the system tool on the device to change the system font.
When I do what he does the problem appears.

It looks like the B4XFont of the label has info about using the icon file data instead of the TypeFace file data.
These are not the same. Even copying the .font form the original label does not change the new label's font icon info.
What you see is the unicode of the system (not default in this case) TypeFont.
 
Upvote 0
Top