Android Question fontsize multiple layout

ivanomonti

Well-Known Member
Licensed User
tempo fa dovevo ricorrere al problema sul font ma che non ho mai preso in considerazione, oggi devo lanciare un prodotto e devo risolvere, non ho capito molto delle varie risoluzioni con i fontsize, chiedo aiuto per risolvere il problema

time ago I had to resort to the problem on the font but I never considered, now I have to launch a product and I have to solve, I did not understand a lot of the different resolutions with fontsize, ask for help to solve the problem

B4X:
Public Sub fonsize(size As Float)

Dim LayoutVal As LayoutValues = GetDeviceLayoutValues 'Preleva dimensione del dispositivo
Dim Dens As Float = Density 'Preleva la densità del dispositivo
Dim Scale As Int = LayoutVal.Scale

If Scale = 1 Then ' for low-density
size = (size * 2) / Dens
Else If Scale = 2 Then ' for medium-density
size = size / Dens
Else If Scale = 3 Then ' for high-density
size = (size * 1.5) / Dens
Else If Scale = 4 Then ' for extra-high-density
size = (size * 2) / Dens
Else If Scale = 5 Then ' for extra-extra-high-density
size = (size * 2.5) / Dens
Else If Scale = 6 Then ' for extra-extra-extra-high-density (launcher icon only; see note above)
size = (size * 3) / Dens
End If

Return size

End Sub
Thank
 

ivanomonti

Well-Known Member
Licensed User
This code is wrong. Font size is already measured in DIPs. You shouldn't scale it as the result will be wrong.

You just need to call AutoScaleAll in the designer script to slightly increase or decrease the font size.
I do not use design but written the Institute code, AutoScaleAll, in this case as the use!!
 

RandomCoder

Well-Known Member
Licensed User
It doesn't matter whether you use the designer or not. Scaling the font size based on the device scale is a programming mistake.
But how should it be done as it's not always possible to use the designer to create views. For instance I'm designing a class that could be used full screen but can just as likely be used on a panel occupying a quarter of the available screen space. Using the MeasureString approach as used here http://www.b4x.com/android/forum/threads/max-text-size.54101/ works but for some reason the MeasureStringHeight needs a slight adjustment and applying a fixed scaling value of 1.75 seems to be the only option other than to use the alternative method which is to use the StringUtils and Reflection libraries which then adds a larger overhead to the class which I am attempting to keep as minimal as possible.
 

Erel

Administrator
Staff member
Licensed User
This is not really related to this thread but there is no good reason to avoid adding a small library such as StringUtils or Reflection. It will not add any overhead.

What I'm trying to explain is that the font unit values, unlike pixel values, are measured in DIPs automatically. You can scale the font size in the same way that auto scale works based on the physical screen size. Scaling it based on the screen scale is a mistake. The layout will not work as you expect it to work.

You can use Klaus Auto Scale module to scale views added at runtime: http://www.b4x.com/android/forum/threads/designer-scripts-autoscale-tutorial.22522/#content
 

RandomCoder

Well-Known Member
Licensed User
Thanks for the explanation and the link (I've not read this before as I mistakenly thought it related only to the designer).
But for me @Erel the real question remains which is why does Canvas.MeasureStringWidth work to produce the correct size of text but Canvas.MeasureStringHeight does not seem to work?
 

RandomCoder

Well-Known Member
Licensed User
Canvas.MeasureStringHeight is based on a lower level API that doesn't behave in the same way that a Label or EditText will behave. StringUtils uses the same measurement process as the views.
Thanks for the clarification.
 
Top