Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to determine android physical screen height in cm or inches?

I need to know exactly how big the screen is on the device in real units of length so I can calculate the acceleration due to gravity in pixels per millisecond.

Is there a method somewhere in the Android API for this?

like image 714
Nathan Avatar asked Feb 03 '10 16:02

Nathan


People also ask

How do I know the size of my Android screen?

1. Display display = getWindowManager(). getDefaultDisplay(); Point size = new Point(); display. getSize(size); int width = size.

How do I find out the size of my screen on my phone?

If you want the display dimensions in pixels you can use this code: Display display = getWindowManager(). getDefaultDisplay(); int width = display. getWidth(); int height = display.

What is screen width and height?

It is usually quoted as width × height, with the units in pixels: for example, 1024 × 768 means the width is 1024 pixels and the height is 768 pixels. This example would normally be spoken as "ten twenty-four by seven sixty-eight" or "ten twenty-four by seven six eight".


2 Answers

Use the following:

    DisplayMetrics dm = new DisplayMetrics();     getWindowManager().getDefaultDisplay().getMetrics(dm);     double x = Math.pow(mWidthPixels/dm.xdpi,2);     double y = Math.pow(mHeightPixels/dm.ydpi,2);     double screenInches = Math.sqrt(x+y);     Log.d("debug","Screen inches : " + screenInches); 

When mWidthPixels and mHeightPixels are taken from below code

private void setRealDeviceSizeInPixels() {     WindowManager windowManager = getWindowManager();     Display display = windowManager.getDefaultDisplay();     DisplayMetrics displayMetrics = new DisplayMetrics();     display.getMetrics(displayMetrics);       // since SDK_INT = 1;     mWidthPixels = displayMetrics.widthPixels;     mHeightPixels = displayMetrics.heightPixels;      // includes window decorations (statusbar bar/menu bar)     if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)     {         try         {             mWidthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(display);             mHeightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(display);         }         catch (Exception ignored)         {         }     }      // includes window decorations (statusbar bar/menu bar)     if (Build.VERSION.SDK_INT >= 17)     {         try         {             Point realSize = new Point();             Display.class.getMethod("getRealSize", Point.class).invoke(display, realSize);             mWidthPixels = realSize.x;             mHeightPixels = realSize.y;         }         catch (Exception ignored)         {         }     } 

See this post for reference: Get screen dimensions in pixels

like image 95
Bghaak Avatar answered Sep 20 '22 00:09

Bghaak


for getting the current size use Math.round at the end.

 DisplayMetrics dm = new DisplayMetrics();     getWindowManager().getDefaultDisplay().getMetrics(dm);     double x = Math.pow(dm.widthPixels/dm.xdpi,2);     double y = Math.pow(dm.heightPixels/dm.ydpi,2);     double screenInches = Math.sqrt(x+y);     Log.d("debug","Screen inches : " + screenInches);  screenInches=  (double)Math.round(screenInches * 10) / 10; 
like image 44
gilix Avatar answered Sep 24 '22 00:09

gilix