Android Question Calculate Physical Screen Size

wonder

Expert
Licensed User
Longtime User
Hi guys!!

Given the resolution and dpi, is it possible to calculate the screen physical size in centimeters?

I know this is simple math, but my brain refuses to work anymore today.
 

Erel

B4X founder
Staff member
Licensed User
Longtime 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.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime 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...
 
Upvote 0

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
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime 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" ?
 
Upvote 0
Top