Get size of system-font in use

Discussion in 'Android Questions' started by moster67, Apr 5, 2013.

  1. moster67

    moster67 Expert Licensed 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.
     
  2. AllyAndroid

    AllyAndroid Member Licensed User

    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: Apr 6, 2013
  3. Erel

    Erel Administrator Staff Member Licensed User

  4. dantheman

    dantheman Member Licensed 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.
     
  5. Erel

    Erel Administrator Staff Member Licensed 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.
     
  6. nfordbscndrd

    nfordbscndrd Well-Known Member Licensed User

    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: Jul 13, 2013
  7. ilan

    ilan Expert Licensed User

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

    thank you
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    This code will take care of Labels, EditText, Buttons, CheckBoxes, RadioBoxes and Spinners:
    Code:
    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: Aug 6, 2015
    Mikonios, Mashiane, le_toubib and 4 others like this.
  9. ilan

    ilan Expert Licensed 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


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

    Erel Administrator Staff Member Licensed User

  11. ilan

    ilan Expert Licensed 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


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

    Code:
    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: Dec 5, 2013
  12. Erel

    Erel Administrator Staff Member Licensed User

    You should use For Each v As View in Activity.GetAllViewsRecursive.
     
    ilan likes this.
  13. ilan

    ilan Expert Licensed User

    wow thanks erel its working

    i do it like this:

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

    Erel Administrator Staff Member Licensed User

    This section will never be executed:
    Code:
    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?
     
  15. ilan

    ilan Expert Licensed 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
     
  16. Alberto Michelis

    Alberto Michelis Active Member Licensed User

    hi,
    Im having the same problem with this code:
    Code:
    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
    Code:
    Activity.LoadLayout("dashboard")                            
        
    Activity.Title = "PortIt"
      
        Panel1.Left=
    Activity.Width/2-Panel1.Width/2
        Panel1.Top=
    Activity.Height/2-Panel1.Height/2
      
        ResetUserFontScale(
    Activity)
     

    Attached Files:

  17. Erel

    Erel Administrator Staff Member Licensed User

    Can you reproduce it in a small project?
     
  18. Alberto Michelis

    Alberto Michelis Active Member Licensed User

    Here it goes...
    It seams to be a Stack Overflow Error
     

    Attached Files:

  19. Erel

    Erel Administrator Staff Member Licensed User

    There was a bug in the code I posted. I've fixed it now.

    Code:
    If v Is Panel Then
      ResetUserFontScale(v) 
    'it was p instead of v
     
  20. Alberto Michelis

    Alberto Michelis Active Member Licensed User

    Any way of use this code in a Code Module to be shared by other modules, how to manage the
    Code:
    Dim access As Accessiblity
     
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