Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android DisplayMetrics returns incorrect screen size in pixels on ICS

Tags:

I've tried this....

DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int fullscreenheight = metrics.heightPixels; int fullscreenwidth = metrics.widthPixels; 

and....

Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y; 

The Gnex has a display of 720×1280. The returned result for width/height (depending on orientation of course) is never 1280. I thought this might have something to do with the on screen navigation bar in Ice Cream Sandwich, so I hide that with :

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); 

...and then started a thread which continuously logs the screen size height/width. Even after the ICS navigation bar was completely gone....the screen size would never report the 1280 value. I would get numbers like this:

width  = 720 height = 1184 

How do you get the true device screen resolution in ICS?

The reason why I need this value is because my app plays video and I would like for the video to take up the entire screen when the device is in landscape orientation. Now I know what you're thinking, why not just give the videoview a value of "match_parent" for the height/width? The reason is because my videos have multiple aspect ratios and I'd like to be able to calculate the correct video size depending on the entire width of the screen.

like image 324
dell116 Avatar asked Jun 12 '12 05:06

dell116


1 Answers

From the answer of Ahmed, this is full code without error:

    int width = 0, height = 0;     final DisplayMetrics metrics = new DisplayMetrics();     Display display = getWindowManager().getDefaultDisplay();     Method mGetRawH = null, mGetRawW = null;      try {         // For JellyBean 4.2 (API 17) and onward         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {             display.getRealMetrics(metrics);              width = metrics.widthPixels;             height = metrics.heightPixels;         } else {             mGetRawH = Display.class.getMethod("getRawHeight");             mGetRawW = Display.class.getMethod("getRawWidth");              try {                 width = (Integer) mGetRawW.invoke(display);                 height = (Integer) mGetRawH.invoke(display);             } catch (IllegalArgumentException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             } catch (IllegalAccessException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             } catch (InvocationTargetException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             }         }     } catch (NoSuchMethodException e3) {           e3.printStackTrace();     } 
like image 110
Frank Nguyen Avatar answered Oct 26 '22 23:10

Frank Nguyen