According to the documentation, the screenWidth and screenHeight properties of the DeviceInfo Object are supposed to take the orientation of the device into account at the time of query.
screenWidth: Specifies the screen width of the device by considering the device orientation at the time of query. The returned value is in device independent pixels (Dp). This attribute is supported in iOS, Android, Windows, and SPA platforms.
However, they do not. At least not on Android.
Steps to reproduce this:
Create a new native project
Open Form1 in DESIGN view
Enter the PROPERTIES tab and select the Action menu
In the Android section, edit the onOrientationChange Action
Add an Add Snippet node
Paste the following code:
alert(`${voltmx.os.deviceInfo().screenWidth} x ${voltmx.os.deviceInfo().screenHeight}`);
Click save
Go to the Form tab
Under Orientation select Both
Build the project and run it on an Android device or emulator
Change the orientation a few times
The alert messages will always have the same value
(See the attached screenshots for the results on my device)
Is there any other way of querying the current device dimensions?
Or is there perhaps a way to query the current orientation of the device?
I am using Volt MX Iris Version 9 SP 2 FP 38, which is the latest version from my understanding.
I've tested this on three emulators, running Android 5.1, 10.0 and 11.0 resprectively. I've also tested this on my physical Mi 11 Ultra which currently runs Android 11. Using the Volt MX Preview App yields the same results.
I would provide you with the sample project I used, but I'm unsure which files and folders would be minimally required.
There is one more thing I found out: The function does return the correct values at the time of query, but only the first time. After that it always returns the same values until the application is closed. In other words, if you start the example application I described above in landscape mode and change the orientation to portrait, deviceInfo() will always yield values for the portrait orientation, because that's when it was first queried. If you instead start the application in portrait mode and switch the orientation to landscape (again, this is when deviceInfo() is first called), it will always return the screen dimensions of the landscape orientation.
@Suhas Bhat I agree that it does make sense to cache the device information, such as the device ID, name, dimensions of the device itself, supported features, etc. because those are constants.
However, screenWidth and screenHeight are not constants. And they should not be treated as such.
Changing orientation is not the only reason why they might change either. Nowadays Android supports having multiple apps on screen at the same time (split screen).
So for whatever purpose the current screen dimensions are required, they should actually represent the current screen dimensions when queried.
Simply put, my use-case revolves around having a dashboard-form that supports both orientations, since some sub-forms require landsape orientation and most require portrait orientation. The reason why I need to query the screen dimensions in the first place, is having an overlay on top of the dashboard that can be swiped up and down at any time. To be able to swipe it down from the top edge of the screen, I need to know the size of the overlay, which is as big as the screen. Basically, I'm doing this: top: calc(-100% + dy) and I need to calculate, what "100%" is in device independant pixels. Well, it's the screenHeight.
Unfortunately, due to the caching, the overlay is sometimes swiped down from two thirds of the screen or from way outside the screen when in a different orientation than when originally queried.