How to automatically set font size to fit device screen size?
I have already read B4X Visual Designer.
I have already read B4X Visual Designer.
Unfortunately, the answer is yes and no, because it will depend on the screen sizes and the layout type and its complexity and there are so many different screen sizes.If I choose the appropriate AutoScaleRate value for my two devices, will it work on other screen sizes.
Dim X,Y As Double
Dim r As Reflector
r.Target = r.GetContext
r.Target = r.RunMethod("getResources")
r.Target = r.RunMethod("getDisplayMetrics")
Log(r.GetField("xdpi"))
Log(r.GetField("ydpi"))
x = GetDeviceLayoutValues.Width/r.GetField("xdpi")
y = GetDeviceLayoutValues.Height/r.GetField("ydpi")
Log("x = "&X)
Log("y = "&y)
Dim fontsize As Int
If y < 4.5 Then
fontsize = 12
Else If y > 4.5 And y < 8 Then
fontsize = 14
Else If y > 8 Then
fontsize = 16
End If
Results for different screensThen you set the font size
It seems to me that visually for a screen size of 1440*2560, fontsize = 14 looks better. Maybe you need to use a coefficient that depends on the diagonal or another parameter?Yes that is correct.
How to use this data in your example?You can modify the comparison values. (5 - 6,2 - ...)
Dim ScreenSize As Float
ScreenSize = GetDeviceLayoutValues.ApproximateScreenSize
Log("ScreenSize : "&ScreenSize)
If ScreenSize < 5 Then
fontsize = 12
Else If ScreenSize > 5 And ScreenSize < 6.2 Then
fontsize = 13
Else If ScreenSize > 6.2 And ScreenSize < 7.5 Then
fontsize = 14
Else If ScreenSize > 7.5 And ScreenSize < 8.7 Then
fontsize = 15
Else If ScreenSize > 8.7 And ScreenSize < 10 Then
fontsize = 16
Else If ScreenSize > 10 Then
fontsize = 17
End If
Log("FontSize : "&fontsize)
Label1.TextSize = fontsize
Thank you!This is simpler and will have the same effect.
When I change AutoScaleRate, nothing changes. Please look at the attached example, what is wrong?You can play with AutoScaleRate.
I did it this way and that way. No difference.You have commented AutoScaleAll you should not !
I'm sorry, I had an error in the manifest. The example works. If I choose the appropriate AutoScaleRate value for my two devices, will it work on other screen sizes. I don't have the opportunity to check.I created a default manifest editor file and the program works now as expected.
With the Android emulator, you can create several screen virtualizations.I don't have the opportunity to check.
Unfortunately, the answer is yes and no, because it will depend on the screen sizes and the layout type and its complexity and there are so many different screen sizes.If I choose the appropriate AutoScaleRate value for my two devices, will it work on other screen sizes.
I would agree 100%. It is plain pain in the a$$ and if OP like his app look pretty on many different devices there is no choice but design many layouts. Autoscaling (or even manual one) usually produce ugly results not only with fonts, but with graphics also. Thus multi-layout.Unfortunately, the answer is yes and no, because it will depend on the screen sizes and the layout type and its complexity and there are so many different screen sizes.
A layout designed for a small screen and stretched to fit onto a big screen will look awful.
For some screen sizes, adjustments would be needed.
If your application should run on smartphones and on tablets it would probably be better to have two layout files one for small screens and one for big screens.
Then, where is the limit between small and big screens ?
The size of big smartphones is almost the same as the size of small tablets.
You need to find the compromise for your application.
Playing with anchors and Designer Scripts.