4

Is it possible to calculate the android screen diagonal dimension in inches.

I have tried below two approaches both is not suitable for my case.

Approach 1:

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int absoluteHeightInPx = displayMetrics.heightPixels;
int absoluteWidthInPx = displayMetrics.widthPixels;     
double diagonalPixels = Math.sqrt((absoluteHeightInPx * absoluteHeightInPx) + (absoluteWidthInPx * absoluteWidthInPx));
double diagonalInInches = diagonalPixels / displayMetrics.densityDpi;

Approach 2:

float actualHeight = absoluteHeightInPx * deviceDensity;
float actualWidth = absoluteWidthInPx * deviceDensity;
float deviceDensity = displayMetrics.density;
float physicalPixelPerInchX = displayMetrics.xdpi;
float physicalPixelPerInchY = displayMetrics.ydpi;
float heightInInches = actualHeight / physicalPixelPerInchY;
float widhtInInches = actualWidth / physicalPixelPerInchX;
double diagonalInInches = Math.sqrt((heightInInches * heightInInches) + (widhtInInches * widhtInInches));

Could you correct if i am wrong.

Thanks, Easwar

Dipak Keshariya
  • 22,009
  • 18
  • 75
  • 127
Easwaramoorthy K
  • 3,757
  • 8
  • 34
  • 61
  • http://stackoverflow.com/questions/2193457/is-there-a-way-to-determine-android-physical-screen-height-in-cm-or-inches – Will Richardson Aug 29 '12 at 07:52
  • Already i have seen the link. but this is not working for me. I have tested in some devices. I am not getting perfect answers. Please help me. – Easwaramoorthy K Aug 29 '12 at 08:03

2 Answers2

7

I use the code below, and it works on my Moto Dey.

 DisplayMetrics metrics=new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    float height=metrics.heightPixels/metrics.xdpi;
    float width=metrics.widthPixels/metrics.ydpi;

    TextView tv=(TextView) findViewById(R.id.textview);
    tv.setText("The screen size is:"+FloatMath.sqrt(height*height+width*width));

The real diagonal length is 3.7" and the code gives out 3.6979072, precise enough. According to the descriptions in SDK, I think my method should work on other devices.

Huang
  • 4,762
  • 3
  • 20
  • 20
  • Actually you have to multiply the heightPixels and widhtPixels with density of the screen. The above derivation will be correct if the density = 1. I think your device has the density 1. Right? – Easwaramoorthy K Aug 31 '12 at 09:36
  • the SDK document says that xdpi is the exact PHYSICAL pixels per inch in the X dimension, and the heightPixels is the absolute height in pixels, so dividing it by the xdpi should get the physical length in inches in X dimension. That's how I consider your question. I don't think density matters here. by the way, my device has the density of 1.5 – Huang Aug 31 '12 at 10:02
  • 1
    it is giving large screen size than the actual screen, is there any possiblity to find exact physical screen size. I tested on 5.5 inches device and it was giving me around 5.8 size. – Dory Nov 19 '13 at 12:41
-1

I have tested both of my approach in 8 android devices. The second is almost accurate.

But for few devices this is not giving proper answer. So I concluded following the second approach.

Easwaramoorthy K
  • 3,757
  • 8
  • 34
  • 61