Android Question Get real size of device display


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:


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:
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")
     Return GetDeviceLayoutValues.Height
   End If
End Sub
As you can see in the code it uses an API only available in Android 17+.


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(
at anywheresoftware.b4j.object.JavaObject.RunMethod(
at com.omnicorp.lcarui.test.main._getrealsize(
at com.omnicorp.lcarui.test.main._activity_create(
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(
at com.omnicorp.lcarui.test.main.afterFirstLayout(
at com.omnicorp.lcarui.test.main.access$000(
at com.omnicorp.lcarui.test.main$
at android.os.Handler.handleCallback(
at android.os.Handler.dispatchMessage(
at android.os.Looper.loop(
at java.lang.reflect.Method.invoke(Native Method)
java.lang.RuntimeException: Method: getRealMetrics not found in: com.omnicorp.lcarui.test.main


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)


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


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.