Android Question Calculate Physical Screen Size

Erel

Administrator
Staff member
Licensed User
Note that this method returns the approximate size which is based on the approximate scale.

If you want to find the exact activity size then you can use:
B4X:
Sub ExactSize As Double
  Dim r As Reflector
  r.Target = r.GetContext
  r.Target = r.RunMethod("getResources")
  r.Target = r.RunMethod("getDisplayMetrics")
  Dim xdpi As Double = r.GetField("xdpi")
  Dim ydpi As Double = r.GetField("ydpi")
  Return Sqrt(Power(100%x / xdpi, 2) + Power(100%y / ydpi, 2))
End Sub
This is only useful if the exact size is really important. For example if you create a ruler app.
 

Peter Simpson

Expert
Licensed User
Note that this method returns the approximate size which is based on the approximate scale.

If you want to find the exact activity size then you can use:
B4X:
Sub ExactSize As Double
  Dim r As Reflector
  r.Target = r.GetContext
  r.Target = r.RunMethod("getResources")
  r.Target = r.RunMethod("getDisplayMetrics")
  Dim xdpi As Double = r.GetField("xdpi")
  Dim ydpi As Double = r.GetField("ydpi")
  Return Sqrt(Power(100%x / xdpi, 2) + Power(100%y / ydpi, 2))
End Sub
This is only useful if the exact size is really important. For example if you create a ruler app.
This will definitely come in handy one day.

Cheers...
 

ArminKH

Well-Known Member
As @Erel said the result for my device is about 4.58 but when i measure my device's diameter by using real ruler(From top corner to bottom corner(above navbar)) the result is 5.31 inches
my phone is huawei g610(named 5")
I set #FullScreen to true and #IncludeTitle to false
Please help me if i am on wrong way
 

incendio

Well-Known Member
Licensed User
Note that this method returns the approximate size which is based on the approximate scale.

If you want to find the exact activity size then you can use:
B4X:
Sub ExactSize As Double
  Dim r As Reflector
  r.Target = r.GetContext
  r.Target = r.RunMethod("getResources")
  r.Target = r.RunMethod("getDisplayMetrics")
  Dim xdpi As Double = r.GetField("xdpi")
  Dim ydpi As Double = r.GetField("ydpi")
  Return Sqrt(Power(100%x / xdpi, 2) + Power(100%y / ydpi, 2))
End Sub
This is only useful if the exact size is really important. For example if you create a ruler app.
Tested on Nexus 7, it returns 7.668"
Shouldn't it returns 7" ?
 
Top