When I implement
WindowManager wm = ((WindowManager)context.getSystemService(context.WINDOW_SERVICE));
Display display = wm.getDefaultDisplay();
m_nDisplayWi
Try this, it works for me.
getWindowManager().getDefaultDisplay().getWidth();
Here is the utility method, you could utilize:
public static int getWidth(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
int width;
if (Build.VERSION.SDK_INT >= 13) {
display.getSize(size);
width = size.x;
} else {
width = display.getWidth();
}
return width;
}
This supports both older and newer devices:
private static Point getDisplaySize(final Display display) {
final Point point = new Point();
try {
display.getSize(point);
} catch (java.lang.NoSuchMethodError ignore) { // Older device
point.x = display.getWidth();
point.y = display.getHeight();
}
return point;
}
Sorry that I'm late to the party, but make sure you're using android sdk 13 or above.
This version works for API < 4, too:
@SuppressWarnings("deprecation")
@TargetApi(VERSION_CODES.HONEYCOMB_MR2)
public static Point getSize(Display display) {
Point point = new Point();
if (sdkInt() >= VERSION_CODES.HONEYCOMB_MR2) {
display.getSize(point);
} else {
point.set(display.getWidth(), display.getHeight());
}
return point;
}
@SuppressWarnings("deprecation")
public static int sdkInt() {
return Integer.parseInt(VERSION.SDK);
}
Try to use that instead :
Display display = getWindowManager().getDefaultDisplay();
Without that, i don't see any problem in this code ...