Android Question Get real size of device display

MoraviaVenus

Member
Licensed User
Hi all,

please, how do I get the real size of the device, where my app is running? For example, when I use Xperia Z3 Compact, which has resolution 720x1280, I would like to get:
- width = 720 px,
- height = 1280 px.

... but when I use few different functions, I always get height = 1184 px instead of expected 1280 px. The functions I use to detect real device height are:
1. 100%y,
2. Activity.Height,
3. os.physicalScreenHeight / os.ydpi,
4. GetDeviceLayoutValues.Height.

My app has following properties:
#Region Activity Attributes
#FullScreen: True
#IncludeTitle: False
#End Region

I assume, that missing 96 px (1280 - 1184) are due to soft keys menu at the bootom and all four commands above return just "useful" screen height.

Please, which command should I use to get the whole screen height of 1280 px? Or, using which command do I get the height of soft keys menu?

Thank you.
 
Last edited:

Erel

Administrator
Staff member
Licensed User
100%y = Activity.Height => returns the size without the action bar, the top bar and the soft keys.

GetDeviceLayoutValues.Height => returns the height without the soft keys bar.

You can use this code to return the screen height including the soft keys bar:
B4X:
Sub GetRealHeight As Int
   Dim p As Phone
   If p.SdkVersion >= 17 Then
     Dim jo As JavaObject
     jo = jo.InitializeContext.RunMethodJO("getWindowManager", Null).RunMethod("getDefaultDisplay", Null)
     Dim metrics As JavaObject
     metrics.InitializeNewInstance("android.util.DisplayMetrics", Null)
     jo.RunMethod("getRealMetrics", Array(metrics))
     Return metrics.GetField("heightPixels")
   Else
     Return GetDeviceLayoutValues.Height
   End If
End Sub
As you can see in the code it uses an API only available in Android 17+.
 

NeoTechni

Well-Known Member
Licensed User
I get

** Activity (main) Create, isFirst = true **
main_getrealsize (java line: 4850)
java.lang.RuntimeException: Method: getRealMetrics not found in: com.omnicorp.lcarui.test.main
at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:367)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:118)
at com.omnicorp.lcarui.test.main._getrealsize(main.java:4850)
at com.omnicorp.lcarui.test.main._activity_create(main.java:624)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at com.omnicorp.lcarui.test.main.afterFirstLayout(main.java:102)
at com.omnicorp.lcarui.test.main.access$000(main.java:17)
at com.omnicorp.lcarui.test.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
java.lang.RuntimeException: Method: getRealMetrics not found in: com.omnicorp.lcarui.test.main
 

NeoTechni

Well-Known Member
Licensed User
I have to target an old version to get the : button, it won't just work even though the code is only running on 4.4 and higher? (I'm trying to get immersive mode to work)
 

NeoTechni

Well-Known Member
Licensed User
Arg, I've now spent 3 hours on this to no avail. I can hardcode my app to 1920 on my nexus 5 and everything else works perfectly. Activity.height = -1 does not work. the OS library and GetDeviceLayoutValues all return the wrong values. Is there API to get the height of the action bar? I can just add it on
 

NeoTechni

Well-Known Member
Licensed User
I gave up and made it so the user can enter the size of their screen. It's a bad solution, but it is a solution.
 
Top