2

I am using this code to get my screen resolution:

//View v
Context ctx = v.getContext();
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

But I get a wrong screen size:

06-03 05:25:47.861: I/Screen Size(5267): 1024x552

My tablet has a 7" screen with 1024x600 resolution. I guess the missing 48 pixels are the ones used by the system bar, but even hiding it, I get that screen size.

Since I am on android 4.1.1 I cannot use getRealSize() method added in API17. How can I get the correct screen size?

Update: The density of my panel is not different from 1.0, so it is not a question duplicate. Size should be correct.

Update2: The following code worked (it is only for API between 13 and 16). It returns 600x1024 so simply make a check on screen orientation to get the proper width/height

Method mGetRawW = Display.class.getMethod("getRawWidth");
Method mGetRawH = Display.class.getMethod("getRawHeight");
int nW = (Integer)mGetRawW.invoke(dp);
int nH = (Integer)mGetRawH.invoke(dp);

I took it from here: Android DisplayMetrics returns incorrect screen size in pixels on ICS

Community
  • 1
  • 1
Vektor88
  • 4,841
  • 11
  • 59
  • 111

2 Answers2

0

You can get display metrics like this

context.getResources().getDisplayMetrics().density;

For different ones than densiy too

This is a method you can override in view to get the screen devices information, goes once per device

 @Override
public void onSizeChanged (int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
screenW = w;
screenH = h;

scaledCardW = (int) (screenW/8); //or whatever scale you need for    bitmaps
scaledCardH = (int) (scaledCardW*1.28);
bitmap = // load your bitmaps

}
JRowan
  • 6,824
  • 8
  • 40
  • 59
0

You can't, but since the system bar is always on the bottom of the screen, you can use this trick to get the real dimensions :

Display display = getWindowManager().getDefaultDisplay();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
int height = display.getWidth();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
int width = display.getWidth();
Dalmas
  • 26,409
  • 9
  • 67
  • 80