Android Question AutoScaleAll and resize Font size

sirjo66

Well-Known Member
Licensed User
Longtime User
Hello to all,
the "resize layout" question is always open and never will be close :(

My program is fixed to landscape orientation and I create a simple layout with some buttons.
But, since I want that this program runs also on tablets, I want to create two variants: one for phone and one for tablets.
So, first I create a variant for phone with attributes 480x320, scale = 1 (160 dpi) and then another variant with attributes 960x600, scale = 1 (160 dpi)
(Note: I have select this variant from "new variant" list, so I think that it is standard)
In each variant I can change button properties (height, width, left, top and so on) but font size if one for two variant (if I change font size of a button in a variant, it change also in the other variant).

In Designer Script tab, there is only AutoScaleAll command
When I run my program on a phone (800x480) I see the text big, but when I run the program on a tablet (1024x600) I see small font.
Do AutoScaleAll resize also the font size or not ??

Here are two screenshot:

phone.png

tablet.png
 

JonPM

Well-Known Member
Licensed User
Longtime User
Where are these screenshots from? Actual devices, emulator, or abstract designer?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Don't write resolutions without the scale value (dpi). The numbers are meaningless.

AutoScaleAll scales the views sizes and font sizes based on the difference between the chosen variant and the device physical size.

I assume that the tablet scale is 1.0 (160 dpi). This means that it almost the same size as the tablet variant. So AutoScaleAll doesn't do anything.

Remove the tablet variant and use the anchors and designer script to move the views as needed.
 
Upvote 0

sirjo66

Well-Known Member
Licensed User
Longtime User
I assume that the tablet scale is 1.0 (160 dpi). This means that it almost the same size as the tablet variant. So AutoScaleAll doesn't do anything.

Many thanks,
but I don't understand because AutoScaleAll resize fonts based on dpi and not on screen size.
I don't know my program where runs, on what dpi.

Well, no problem, it's time to develop my AutoScaleAll routine, it will resize all objects on layout based only on screen resolution.
Is there already a library and/or routine that works ??

From where can I start ??

Many thanks
Sergio

Edit: AutoScaleAll is perfect for top/left/width/height proprieties, I need only to change TextSize
 
Last edited:
Upvote 0

davfla

Member
Licensed User
Longtime User
Maybe you can work with this simple method to get the font size.

B4X:
Sub Activity_Create(FirstTime As Boolean)
  
    Dim but As Button
    but.Initialize("")
    Activity.AddView(but, 10%x, 20%y, 80%x, 7%y)
    but.Text = "I'm a sweet button."
  
    'In your example the MaxFontHeight can be the button height
    Dim Perc As Int    'The Fontsize should be only 70% of the Button Height
    Perc = (but.Height / 100) * 70
    but.Textsize = GetFontSize(Perc)
End Sub


Sub GetFontSize(Height As Int) As Int
    Dim lbl As Label : lbl.Initialize("")
    Dim strUtil As StringUtils                'Needed for MeasureTextHeight
    Dim Ok As Boolean                        'Only needed for the loop
    Dim FontSizeMAX As Int = 40                'Start with the a max
  
    'Paint the test label
    Activity.AddView(lbl, 0, 0, 100%x, 1%y)
    lbl.Text = "I'M A NONSENSE TEXT"
    lbl.TextSize = FontSizeMAX
  
    'Runs the loop until the height of the label is lower or similar to the passed height
    Do Until Ok
        lbl.Height = strUtil.MeasureMultilineTextHeight(lbl, lbl.Text)
        If lbl.Height > Height Then
            lbl.TextSize = lbl.TextSize - 1
        Else
            lbl.RemoveView            'Remove the test label
            Return lbl.TextSize        'Returns the font size
            Ok = True
        End If
    Loop
    Return 0        'Is only to prevent the IDE failure "Not all codes paths return a value."
End Sub

David
 
Upvote 0

sirjo66

Well-Known Member
Licensed User
Longtime User
Don't write resolutions without the scale value (dpi). The numbers are meaningless.

Thanks Erel, now I have understand a little more.
I have added:
B4X:
Dim lv As LayoutValues
lv = GetDeviceLayoutValues
Log(lv)

My phone is 800x480 scale = 1.5 (real is 233 dpi) , and my tablet is 1024x600 scale = 1 (real is 170 dpi)
When my program runs on tablet, it see that the scale is the same of the variant, so it don't do anything (about font size) and I see the font small, but when it runs on phone, AutoScaleAll resize the font more big, because it found a scale of 1.5
The problem is that I want re-arrange layout (and font size) on screen resolution, not on dpi

Maybe you can work with this simple method to get the font size.

Many thanks David, I like this code, I will try it

Sergio
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
As I previously wrote:

AutoScaleAll scales the views sizes and font sizes based on the difference between the chosen variant and the device physical size.

It has nothing to do with the screen dpi.

I think I have solved in a personal way, but I would like to understand better.

This your statement...

If I create a variant 500x500 and a button 100x100, TextSize 10, and the device is 1000x1000, how AutoScaleAll will calculate? What the end "result" will be? And what if I change AutoScaleRate from 0.3 to 0.9?


Thank you
 
Upvote 0

sirjo66

Well-Known Member
Licensed User
Longtime User
As I previously wrote:

AutoScaleAll scales the views sizes and font sizes based on the difference between the chosen variant and the device physical size.

It has nothing to do with the screen dpi.

Erel, I know that you have a lot of patience, thanks for this.

I create a new layout (without variant) as you see in this image, with only 5 button

layout.png

If I use anchors BOTH (up, down, left and right) on my tablet (1024x600 scale = 1) the result is:

anchor_both.png

If I use anchors only on LEFT and TOP, the result is:

anchor_left_top.png
I cry :(

Sergio
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
@luca please start a new thread for this question (don't forget to write the scale values).

You need to understand what anchors mean.

It doesn't make sense to set the vertical anchors to BOTH in your layout.

A good way to see how anchors behave is by putting the views inside a panel and then playing with the panel size. For example: https://www.b4x.com/android/forum/threads/designer-anchors-video-example.36507/#content

It does make sense to use BOTH for the horizontal anchors of all these views except the small button. It should be anchored to the right.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
@luca please start a new thread for this question (don't forget to write the scale values).

Probably I will open a new thread. I have not specified the scale because of this your sentence:
AutoScaleAll scales the views sizes and font sizes based on the difference between the chosen variant and the device physical size.

It has nothing to do with the screen dpi.

Thanks
 
Upvote 0

sirjo66

Well-Known Member
Licensed User
Longtime User
It does make sense to use BOTH for the horizontal anchors of all these views except the small button. It should be anchored to the right.

Solved by adding AutoScaleRate(0.8) before AutoScaleAll

But 0.8 what is ??

How can calculate it ??

Sergio
 
Upvote 0
Top