Android Question 1dip value (calculation) - problem

woniol

Active Member
Licensed User
Longtime User
Hi all,
I'm back with my problem described here: https://www.b4x.com/android/forum/threads/1dip-calculation.98422/
It happened again after some time. As I mentioned it's very rare.
This time I managed to catch this output for logs:
B4X:
Sub Activity_Create(FirstTime As Boolean)
  
    Log(Activity.Height&" x "&Activity.Width)
    Log("1dip "&1dip)
    Log(GetDeviceLayoutValues)
    Log(GetDeviceLayoutValues.ApproximateScreenSize)

result:
B4X:
2560 x 1440
1dip 1
1440 x 2560, scale = 4.0 (640 dpi)
4.589389937671455

how is it possible that GetDeviceLayoutValues returns correct values and 1dip is badly calculated?

PS. It happens on: Galaxy S7(Android 8), Galaxy A8(Android 8) and galaxy J5(Android 7), so it doesn't weem to be system or phone related problem.
 

mindful

Active Member
Licensed User
You should use DipToCurrent to get the dips converted in pixels.

To get the DPI of your screen (scale) you should use GetDeviceLayoutValues.Scale
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
try this:
B4X:
Sub IntToDIP(Integer As Int) As Int
   Dim r As Reflector
   Dim scale As Float
   r.Target = r.GetContext
   r.Target = r.RunMethod("getResources")
   r.Target = r.RunMethod("getDisplayMetrics"))
   scale = r.GetField("density")
  
   Dim DIP As Int
   DIP = Integer * scale + 0.5
   Return DIP
End Sub
 
Upvote 0

woniol

Active Member
Licensed User
Longtime User
try this:
B4X:
Sub IntToDIP(Integer As Int) As Int
   Dim r As Reflector
   Dim scale As Float
   r.Target = r.GetContext
   r.Target = r.RunMethod("getResources")
   r.Target = r.RunMethod("getDisplayMetrics"))
   scale = r.GetField("density")
 
   Dim DIP As Int
   DIP = Integer * scale + 0.5
   Return DIP
End Sub

Thank you, but where shoul I use it?
I use dip in the layouts all over the app,
and the problem is that sometimes it's calculated to 1, while normaly it shoud be 4 , when the app starts, checked with:
B4X:
Log("1dip "&1dip)

It's the same problem as reported in this thread: https://www.b4x.com/android/forum/threads/android-layout-size-not-loading-properly.98507/
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
It could be used instead of Dip.
Unfortunately I can not tell you why it gives you wrong value to me never happened.
I can not tell you if the problem arises from the device or from the command.
However this method is very precise.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    MyPanel.Width=IntToDIP(100)
End Sub

Sub IntToDIP(Integer As Int) As Int
   Dim r As Reflector
   Dim scale As Float
   r.Target = r.GetContext
   r.Target = r.RunMethod("getResources")
   r.Target = r.RunMethod("getDisplayMetrics"))
   scale = r.GetField("density")
 
   Dim DIP As Int
   DIP = Integer * scale + 0.5
   Return DIP
End Sub

see this thread https://www.b4x.com/android/forum/threads/convert-integer-to-dip.18800/#content
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
I see how this can happen and I will make it more robust in the next version.
Erel -
If to follow your recomendations, do we need to worry anyway ? I never noticed something wrong with dips. Meanwhile I do not use layouts and do not use dip inside globals and process_globals.
 
Upvote 0

woniol

Active Member
Licensed User
Longtime User
Thank you,
I will check Process_Globals declarations for dips. Move it outside Process_Globals if I find any and report back.
 
Upvote 0

woniol

Active Member
Licensed User
Longtime User
I'll check that and report on friday, I have no access to B4A right now.
But to be honest, I always avoid using code in Process_Globals. Need to check anyway. Thanks
 
Upvote 0

woniol

Active Member
Licensed User
Longtime User
I just checked and didn't find any code other than declarations.
Just something like this in the Main:
B4X:
Sub Process_Globals
    Public SubVer As String = "161018"
    Dim pw As PhoneWakeState
    Private xui As XUI
    Private prevIntent As Intent
    Private currentPage As Int
End Sub

and one assigment in Globals:
B4X:
Sub Globals
    Dim ActivitySize As Int = GetDeviceLayoutValues.ApproximateScreenSize

I'm not sure if any of these can cause described issue...
 
Upvote 0

woniol

Active Member
Licensed User
Longtime User
Still not there, i'm checking spam as well.
I receive the 'new reply to watched thread' notifications as well as info about new B4A versions, so my email address should be ok.
 
Upvote 0

woniol

Active Member
Licensed User
Longtime User
I got the files. There is common created. in the unfiltered logs now.
I'll report it back if the issue appears or if it's ok, after some time of using my app.
Thanks for your help.
 
Upvote 0

woniol

Active Member
Licensed User
Longtime User
I've sent you an email with updated core libraries. Please try them. They should solve this issue.

Check the unfiltered logs. It should show this message:
common created.

If you are not seeing it then you are still using the old libraries.
It's been a week since I compiled with new core libraries and installed on tree phones.
So far the described issue haven't appeared.
It looks that the change in core libraries solved it.

Thanks for help and please provide it with the new version.
 
Upvote 0
Top