17

When I implement

WindowManager wm = ((WindowManager)context.getSystemService(context.WINDOW_SERVICE));
Display display = wm.getDefaultDisplay();
m_nDisplayWidth = display.getWidth();
m_nDisplayHeight    = display.getHeight();

I can run fine, but when I implement getSize I get runtimeError

Point size = new Point();
display.getSize(size); //Error right here
m_nDisplayWidth = size.x;
m_nDisplayHeight = size.y;

Logcat:

03-11 01:45:25.865: E/AndroidRuntime(18835): FATAL EXCEPTION: main 
03-11 01:45:25.865: E/AndroidRuntime(18835): android.view.InflateException: Binary XML file line #7: Error inflating class com.brain.development.GameRun
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.view.LayoutInflater.createView(LayoutInflater.java:518)
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:215) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.app.Activity.setContentView(Activity.java:1679) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at com.brain.development.BrainDevelopmentActivity$1.onClick(BrainDevelopmentActivity.java:25) 03-11 01:45:25.865: E/AndroidRuntime(18835):  at android.view.View.performClick(View.java:2582) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.view.View$PerformClick.run(View.java:9252) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.os.Handler.handleCallback(Handler.java:587) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.os.Handler.dispatchMessage(Handler.java:92) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.os.Looper.loop(Looper.java:130) 03-11 01:45:25.865: E/AndroidRuntime(18835):     at android.app.ActivityThread.main(ActivityThread.java:3691) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at java.lang.reflect.Method.invokeNative(Native Method) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at java.lang.reflect.Method.invoke(Method.java:507) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at dalvik.system.NativeStart.main(Native Method) 
03-11 01:45:25.865: E/AndroidRuntime(18835): Caused by: java.lang.reflect.InvocationTargetException 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at java.lang.reflect.Constructor.constructNative(Native Method) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at java.lang.reflect.Constructor.newInstance(Constructor.java:415) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at android.view.LayoutInflater.createView(LayoutInflater.java:505) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    ... 19 more 
03-11 01:45:25.865: E/AndroidRuntime(18835): Caused by: java.lang.NoSuchMethodError: android.view.Display.getSize 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at com.brain.development.GameRun$GameThread.<init>(GameRun.java:46) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    at com.brain.development.GameRun.<init>(GameRun.java:97) 
03-11 01:45:25.865: E/AndroidRuntime(18835):    ... 22 more
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
sayhaha
  • 769
  • 3
  • 12
  • 25

7 Answers7

70

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;
}
Tamara
  • 840
  • 7
  • 12
  • getWidth and getHeight are deprecated methods now since arrival of getSize method, but its usable only for api level 13 and above, so no option but to use deprecated method itself for older devices. +1 for ur answer. thanks – Zoombie Oct 16 '12 at 06:23
4

Method to get Display Size for all devices:

private static Point getDisplaySize(final Display display) {
    Point point = new Point();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { // API LEVEL 13
        display.getSize(point);
    } else {    
        point.x = display.getWidth();
        point.y = display.getHeight();
    }
    return point;
}

gettin Size:

WindowManager wm = ((WindowManager)context.getSystemService(context.WINDOW_SERVICE));
Display display = wm.getDefaultDisplay();
Point myPoint = getDisplaySize(display);    
Log.i("***Size X::", String.valueOf(myPoint.x));
Log.i("***Size Y::", String.valueOf(myPoint.y));
alexrnov
  • 2,346
  • 3
  • 18
  • 34
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
2

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);
}
KitKat
  • 1,495
  • 14
  • 15
1

Try this, it works for me.

getWindowManager().getDefaultDisplay().getWidth();
prolink007
  • 33,872
  • 24
  • 117
  • 185
Chirag Patel
  • 11,416
  • 3
  • 26
  • 38
  • getWidth() is deprecated http://developer.android.com/reference/android/view/Display.html#getWidth() – pahan Mar 29 '13 at 10:26
0

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;
}
alexrnov
  • 2,346
  • 3
  • 18
  • 34
Ash
  • 1,391
  • 15
  • 20
0

Try to use that instead :

Display display = getWindowManager().getDefaultDisplay();

Without that, i don't see any problem in this code ...

ChapMic
  • 26,954
  • 1
  • 21
  • 20
0

Sorry that I'm late to the party, but make sure you're using android sdk 13 or above.

worr
  • 759
  • 1
  • 5
  • 9