Android Question i will have same visual button size at differnt devices.

MarkusR

Well-Known Member
Licensed User
Longtime User
i saw the tutorial about dip but i don't get it.
as example for a button:
me thought if me input a size value width and height in designer it is dip and the visual size is the same at all devices, but it is not.
me tested also with autoscale on/off.

portrait:
i have a 10" tablet 800x1216 scale 1.33125 (213 dpi)
and i have a phone 720x1184 scale 2 (320 dpi)

in designer i had input 720x1280 scale 2 and then there was a popup and it is saved as 360x640 scale 1 (160 dpi) variant.
 
Last edited:

Cableguy

Expert
Licensed User
Longtime User
Your question is...?

BTW: the scale factor means just that, your base layout, factor 1, will automatically be scaled to factor 2 in the targeted device.
There are many ways to ensure the layout looks about the same in all screens, one of them is to use percentages (x% and y%)
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
Your question is...?
how to get same visual button size at differnt devices?

it make no sense so scale the button bigger as my finger need to click.
the smallest device is my phone, if me can use this visual size there i can use the same at tablet and i do not need to waste the screen.
as example a icon button with 1x1 cm.
the button should look 1x1 cm at my phone and at my tablet.

There are many ways to ensure the layout looks about the same in all screens, one of them is to use percentages (x% and y%)
percentages is not what i want but i used it to center something at screen :)
10% from screen is 10% from screen but it will be different in size if u compare a 23" monitor with a phone in landscape.
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Then you have a real estate problem! A screen that look good with about 10 views on screen in a 6" phone will have too much free space if you don't scale the views!
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
but in this free space i can show data, more items or more rows.
i do not want everything 1:1
as example i can show the camera view inside "streched" with the help of anchors.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
then you just use "normal" values (dpi), but getting a 1cm button (or any other value) to be the same on all screens is a very difficult task.

I remember there's a Getphysicalscreensize method (not sure of the method's name but should be close enough to find it) with wich you can retrieve the (almost) true size of the screen.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
me thought if me input a size value width and height in designer it is dip and the visual size is the same at all devices, but it is not.
me tested also with autoscale on/off.
How did you test it?
dip = density independant pixel
In the Designer, the dimensions are in dip values.
If you comment AutoScaleAll then the dimensions are almost the same, but not exactly the same, on the different devices.
The 'standard' density in Android is 160 dpi, dots per inch, pixels per inch.
The standard scales in Android are 0.75, 1, 1,5, 2, 3 (120, 160, 240, 320, 480 dots)
But the different devices don't have exactly the standard density, therefore there are differences.
A view with 160 / 160 dip pixels will have a dimension of, theoretical, 1 inch, 25.4 mm.
But if the real density is 180 pixels per inch, this density is considered by Android as 160.
But the real dimension on the device is 25.4 / 180 * 160 = 22.6 mm!
If you use AutoScaleAll, then the dimensions are increased depending on the AutoScaleRate which is equal to 0.3 by default.
 
Last edited:
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
hmm,
i will investigate end this week.
as i understand from here density-independent pixel is a physical size.
https://en.wikipedia.org/wiki/Device-independent_pixel

How did you test it?
i tested with autoscale on/off at both of my devices.
the button size was different.
i guess if the apps starts it do not get the correct screen informations.
i had also the problem that the "what you see is what you get" was wrong in compare with preview and the running app.
 
Upvote 0

CaptKronos

Active Member
Licensed User
Longtime User
Perhaps what you are after is the physical screen size of the device. You can use it to ensure that the physical sizes of views are similar across different sized devices.

B4X:
Dim lv As LayoutValues = GetDeviceLayoutValues
Log(lv.ApproximateScreenSize)
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
as i understand from here density-independent pixel is a physical size.
Yes.
density independnt pixel means the following:
If you set the width of a view to 160dip in the Designer or in the code,
you will get a real width in device pixels which are 160 * scale.

dip is a shortcut of the DipToCurrent keyword, from the help:
DipToCurrent

Method

Scales the value, which represents a specific length on a default density device (Density = 1.0),
to the current device.
For example, the following code will set the width value of this button to be the same physical size
on all devices.
Button1.Width = DipToCurrent(100)

Note that a shorthand syntax for this method is available. Any number followed by the string 'dip'
will be converted in the same manner (no spaces are allowed between the number and 'dip').
So the previous code is equivalent to:
Button1.Width = 100dip 'dip -> density independent pixel

Returns : Int

DipToCurrent(Length As Int)

the button size was different.
How much is the difference?

This code will not solve the problem:
B4X:
Dim lv As LayoutValues = GetDeviceLayoutValues
Log(lv.ApproximateScreenSize)
It gives the approximate dimension of the screen diagonal in inches, very approximate from my experience.
The AutoScale methode is based on this.

There does exist a method to get the real density of the device, accessible with the JavaObject.
B4X:
Private xdpi, ydpi As Float
Private jo As JavaObject
jo = jo.InitializeContext
jo = jo.RunMethod("getResources", Null)
jo = jo.RunMethod("getDisplayMetrics", Null)
xdpi = jo.GetField("xdpi")
ydpi = jo.GetField("ydpi")
Log(xdpi & " / " & ydpi)
Log(GetDeviceLayoutValues.Scale)
Log(160 * GetDeviceLayoutValues.Scale)
On my Samsung Galaxy S8:
xdpi = 422.0302429199219
ydpi = 423.96978759765625
But GetDeviceLayoutValues.Scale = 3
Therefore the density taken into account by Android is 480, instead of 423, which makes a difference of about 12%.
This means that the physical dimensions are different on my device than on others with a real 480dpi screen, for a given dip value.

Don't mix up dpi (dots per inch) and dip (density independant pixel) !
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
For example, the following code will set the width value of this button to be the same physical size
on all devices.
Button1.Width = DipToCurrent(100)

that what i mean, the button width & height in designer are in dip without dip after value in property,true? so i expect what you are said.
same physical size
on all devices.

How much is the difference?
it look double size at my tablet.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
... and maybe double each side, so quadruple size, right?

[Although this is not the question... I'm still dreaming the day when all devices will have ratio 16:9, like TVs :mad:]

double diagonal :) end this week i have more details.

better
ps4 have vr with sphere view :)
its possible to overlay the real view with virtual view.
i think monitors / tv's and device screens everywhere are unnecessary.
it can just replace with cheap "eye glasses".
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
the button width & height in designer are in dip without dip after value in property,true?
Yes.

it look double size at my tablet.
Seems too big to me.
What layout variant do you use?
Can you post a layout file.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
@klaus
Can you post a layout file.
later this week, i am on a team event this week.

off topic
@LucasMs
Ok, but you can not go around streets with that type of "display" (and maybe driving a car).
yes, sure!
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
OT
"next step": no more jobs for humans, just for robots
postman robots are planned. also autonomous cars & buses.
i saw also a kitchen robot arm for cooking.
this year also a burger machine :)

robots needs software, we have a save job. :D
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
robots needs software, we have a save job. :D
No, because not many IT experts will be required, but a few super specialized ones.

So, everything will be done by robots; but to whom will the products be sold, to people without work and therefore without money?

The only solution, the best, would be that everyone worked only 20 hours a week (and in the future even less), getting a decent salary. But this is not happening; now it is happening that few people earn a lot of money, increasing the production of consumer goods that fewer and fewer people can buy. And it will always be worse in the next few years; until the moment when ... I can not write it!
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
OT
this progress will come slowly.
good 3d printers will change the world.
the game "deus ex human revolution" had a good story about future.
if you see what we get the last 100 years i am very nosy what comes next in future.
 
Upvote 0
Top