Get size of system-font in use

moster67

Expert
Licensed User
Longtime User
The other day I received a report (along with screenshots) from a user saying that certain labels on his Galaxy S3 were truncated or simply did not show up correctly. I found this rather odd since my developer-phone is also a Galaxy S3 and of course I had made sure that everything (my layouts) looked OK, not only on my S3, but also in other resolutions.

First I thought the user might have installed a custom-ROM with different system-fonts but then it struck me the user could have enlarged the system-fonts in the display-settings and indeed he had.

In this specific case, I was able to fix it easily by enlarging the labels but I am wondering if the designer-script and the auto-scale features take into consideration these situations when a user enlarge the system font-size? Any ideas?

If not, is it possible to get the size of the system-font in use? Of course I don't want to override this because if a user has deliberately increased the font-size, there is surely a good reason for it (sight-problems etc) and perhaps we developers should foresee that there can be problems with layouts in these situations and we should adapt our code/design somehow although this might not always be possible (short of screen-space) and in these cases we could launch a messagebox or similar informing the user there might be layout-issues.
 

AllyAndroid

Member
Licensed User
Longtime User
I am wondering if the designer-script and the auto-scale features take into consideration these situations when a user enlarge the system font-size?

I just tested this with one of my apps that uses Auto Scale by changing the font on my Droid 4 from Normal to Huge. When I ran my app, the text was too large and truncated. So my guess is that the Auto Scale and Designer Script does not take it into consideration.

I would also like to know how to get this information.

Would it be this setting?

http://developer.android.com/reference/android/provider/Settings.System.html#FONT_SCALE
 
Last edited:
Upvote 0

dantheman

Member
Licensed User
Longtime User
Then what? My app looks like junk on large font, and fine in normal. Can I enforce normal font in code? Would really love some help please.
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Yes, you can create a simple loop that removes the user scale. However the question is whether you should do it as this is the user decision to increase the font size.

Because of this thread, I'm putting some code at the start of Activity Create to check font scale size and if it's not 1, giving the user the option of quitting or letting the app scale it back to 1 and then setting it back when he exits the app.

What is the "simple loop that removes the user scale."

*Edit: Never mind. I finally found your post on the subject and found that it is not changing the font size scale but the text size assigned to all views in the app with text, which I don't think will work for me.
 
Last edited:
Upvote 0

ilan

Expert
Licensed User
Longtime User
Yes, you can create a simple loop that removes the user scale. However the question is whether you should do it as this is the user decision to increase the font size.

how do i do this erel?
i want to set the font to normal, how can i do it?

thank you
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code will take care of Labels, EditText, Buttons, CheckBoxes, RadioBoxes and Spinners:
B4X:
Sub Process_Globals
   Dim access As Accessiblity
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   ResetUserFontScale(Activity)
End Sub

Sub ResetUserFontScale(p As Panel)
   For Each v As View In p
     If v Is Panel Then
       ResetUserFontScale(v)
     Else If v Is Label Then
       Dim lbl As Label = v
       lbl.TextSize = lbl.TextSize / access.GetUserFontScale
     Else If v Is Spinner Then
       Dim s As Spinner = v
       s.TextSize = s.TextSize / access.GetUserFontScale
     End If
   Next
End Sub
 
Last edited:
Upvote 0

ilan

Expert
Licensed User
Longtime User
thank you erel
but my app just freeze and closing when i use your code
so i am just informing now on the start of my app that it recommendet to change the font to normal


B4X:
Sub ResetUserFontScale(p As Panel)

Try
Dim fscale As Double
fscale = NumberFormat(access.GetUserFontScale,0,2)
If fscale <> 1 Then
Msgbox("על מנת שהאפליקציה תעבוד כראוי מומלץ לשנות את גודל הפונט בהגדרות של המכשיר שלכם לרגיל" ,"אזהרה")
End If
 
Catch
DateTime.DateFormat = "dd/MM/yyyy"
loglist.Add ("menu | fontscale " & DateTime.Date(DateTime.Now))
End Try
 
End Sub
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
hi erel, 've Been a while

the code that makes my app freeze is this

If v IsPanelThen
ResetUserFontScale(p)
....

so i delete it and do like this


B4X:
Sub ResetUserFontScale'(p As Panel)


Dim fscale As Double
fscale = access.GetUserFontScale

    For Each v As View In activity
        If v Is Label Then
          Dim lbl As Label = v
          lbl.TextSize = NumberFormat2(lbl.TextSize / fscale,1,0,0,False)
        Else If v Is Button Then
          Dim s As Button = v
          s.TextSize = NumberFormat2(s.TextSize / fscale,1,0,0,False)
        End If
    Next
   ' DoEvents

End Sub

the problem is that views that are in panels are not checked only views in my activity

so i did like this and it works

B4X:
Dim fscale As Double
fscale = access.GetUserFontScale


    For Each v As View In pbottom '(pbottom is a panel)
        If v Is Label Then
          Dim lbl As Label = v
          lbl.TextSize = NumberFormat2(lbl.TextSize / fscale,1,0,0,False)
        Else If v Is Button Then
          Dim s As Button = v
          s.TextSize = NumberFormat2(s.TextSize / fscale,1,0,0,False)
        End If
    Next
    'DoEvents

its strange that if i run for each .. in activity he only checked vies that are in my activity but not in panels

do i really have to search each panel for his views?
 
Last edited:
Upvote 0

ilan

Expert
Licensed User
Longtime User
wow thanks erel its working

i do it like this:

B4X:
Dim fscale As Double
fscale = access.GetUserFontScale


If fscale > 1 Then

    For Each v As View In activity.GetAllViewsRecursive
        If v Is Label Then
          Dim lbl As Label = v
          lbl.TextSize = NumberFormat2(lbl.TextSize / fscale,1,0,0,False)
        Else If v Is Button Then
          Dim s As Button = v
          s.TextSize = NumberFormat2(s.TextSize / fscale,1,0,0,False)
        End If
    Next

End If

now if the user set the font bigger the normal then it change all vies textbox to normal

one thing it doesnot do, the listviews items, clv items, msgbox,.. are still big
how can i control them?

thank you
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This section will never be executed:
B4X:
Else If v IsButton Then 
 Dim s AsButton = v
 s.TextSize = NumberFormat2(s.TextSize / fscale,1,0,0,False)EndIf
A Button is a subclass of Label. So Label will catch it as well.

You can just remove it.

You will need to implement similar solution for ListView and Spinner (and maybe CustomListView).

However if the user has changed the font settings why do you want to force him to use the standard font size?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
because all labels not viewing correctly
text is not fully visiable, instead of showing 1 line text i get 2 line text because text is to big

Screenshot_2013-12-05-12-58-32.png



Screenshot_2013-12-05-12-58-47.png
 
Upvote 0

Alberto Michelis

Well-Known Member
Licensed User
Longtime User
hi,
Im having the same problem with this code:
B4X:
If v IsPanelThen
ResetUserFontScale(p)
Ive tested it in many activities with differents views.
And it make all to freeze, may be because its a recursive call?
attached goes the ofensive .bal
The code in the Activity create is
B4X:
    Activity.LoadLayout("dashboard")                            
    Activity.Title = "PortIt"
  
    Panel1.Left=Activity.Width/2-Panel1.Width/2
    Panel1.Top=Activity.Height/2-Panel1.Height/2
  
    ResetUserFontScale(Activity)
 

Attachments

  • dashboard.bal
    9.3 KB · Views: 475
Upvote 0

Alberto Michelis

Well-Known Member
Licensed User
Longtime User
Any way of use this code in a Code Module to be shared by other modules, how to manage the
B4X:
    Dim access As Accessiblity
 
Upvote 0
Top