Problem with view phisical size

marcick

Well-Known Member
Licensed User
Longtime User
I want that all the views have a fixed size in mm on any device.
I have defined just one layout variant: 320x480 scale 1

I have a button with an height of 40dip
Because "scale1" should mean 160dip per inches, 40 dip means 25.4x40/160=6.35mm

Running this on a device that really has 320x40 scale 1, the button height is really about 6.3mm

Running this on another device that has scale 2, I would expect to measure 12.7mm but i see instead about 8mm

I have tryed many methods to read by code the scale of the device and the result was always 2.

So, it seems the rule of 160dip per inches is not true for different scale than 1
Also tested on another device with scale 1.5 and not correspond.

So, I need a method that allow me to have always 6.35 mm of button height (and all other views the problem exists) on any device. How to do ?

Marco
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The scale value is an approximate value. The reason for this approximation is to avoid having an endless range of scales.

If you need the exact values you can use this code:
B4X:
Dim r As Reflector
r.Target = r.GetContext
r.Target = r.RunMethod("getResources")
r.Target = r.RunMethod("getDisplayMetrics")
Log(r.GetField("xdpi")) 'pixels per inch in the X dimension
Log(r.GetField("ydpi")) 'pixels per inch in the Y deminsion
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
I was so happy but .. anyway it does not correspond:

On the device that has an approximate scale of 2, that code returns a "real" scale of 1.55.
Ok, but 40 dip should give a real height of 6.35x1.55=9.84mm and I measure 7.5 instead.
 
Upvote 0
Top