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


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

the cloned icon font changes to a strange font


proof-of-concept project is attached.
 

Attachments

  • TestFont.zip
    9.8 KB · Views: 58

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
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User

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

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
 
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
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
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
 
Last edited:
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
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

 

Attachments

  • TestFont.zip
    10 KB · Views: 48
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


my sample with your above code:




logs
 
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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…