18

Hey I need to get the width of the screen in my application. The application will run on 2.1 and upwards. I have set it up like the one below. The method is deprecated and i should proabably use getSize or a other way. But the question is: Will this work on android versions like 3.0+ and 4.0+, or will it make the app crash. I have used a deprecated method in a thread before and it made the app crash on ice cream devices. Will the method below work ?

 Display display = getWindowManager().getDefaultDisplay(); 
     int width = display.getWidth();
     int height = display.getHeight();

EDIT:

I have tried the getSize but i dont get it to work:

  Display display = getWindowManager().getDefaultDisplay();
      Point size = new Point();
      display.getSize(size);
      int width = size.x;
      int height = size.y;
Ukjent
  • 823
  • 2
  • 9
  • 26

5 Answers5

26

I am not sure but this may work:

if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
    Display display = getWindowManager().getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();
} else {
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
}
ol_v_er
  • 27,094
  • 6
  • 48
  • 61
AndDev
  • 316
  • 1
  • 2
  • 7
12

I don't know whether these deprecated methods will work on Android 3 and 4. The best way to tell is to test on an emulator.

But, the safest method here for max compatibility will be to try one method using reflection, and fall back to the other. Essentially, you could make your own version of getSize() that can't fail. I can't test this atm, but it might look like this:

void overrideGetSize(Display display, Point outSize) {
    try {
      // test for new method to trigger exception
      Class pointClass = Class.forName("android.graphics.Point");
      Method newGetSize = Display.class.getMethod("getSize", new Class[]{ pointClass });

      // no exception, so new method is available, just use it
      newGetSize.invoke(display, outSize);
    } catch(NoSuchMethodException ex) {
      // new method is not available, use the old ones
      outSize.x = display.getWidth();
      outSize.y = display.getHeight();
    }
}

Then of course just call it with something like

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
overrideGetSize(display, size);
Dan J
  • 25,433
  • 17
  • 100
  • 173
Steve Blackwell
  • 5,904
  • 32
  • 49
  • In order to get a method through reflection, you have to pass in an array of the classes of its parameters. You have to specify the parameters in case the method is overloaded. See docs for [`getMethod()`](https://developer.android.com/reference/java/lang/Class.html). So we're creating an array of just one element for the `Point` class, since `getSize()` takes just that one parameter. Does that help? – Steve Blackwell May 18 '12 at 22:28
  • 1
    Or you can use the non depreciated [Display getMetrics()](http://developer.android.com/reference/android/view/Display.html#getMetrics%28android.util.DisplayMetrics%29) – Johan vdH Nov 04 '13 at 20:59
  • 1
    @JohanvdH - You're right. `getMetrics()` has been around since API 1 and looks like it returns the same values. That's probably the simplest way to go. – Steve Blackwell Nov 05 '13 at 03:55
3

I've extended Steve's helpful code so that Eclipse doesn't give any warnings or errors, and I've also restructured it slightly. Since the Point class has been present since API level 1 I didn't see much benefit in creating it through reflection.

  final static String mTAG = "MYTAG";

  // Cope with deprecated getWidth() and getHeight() methods
  Point getSize(Display xiDisplay) 
  {
    Point outSize = new Point();
    boolean sizeFound = false;

    try 
    {
      // Test if the new getSize() method is available
      Method newGetSize = 
        Display.class.getMethod("getSize", new Class[] { Point.class });

      // No exception, so the new method is available
      Log.d(mTAG, "Use getSize to find screen size");
      newGetSize.invoke(xiDisplay, outSize);
      sizeFound = true;
      Log.d(mTAG, "Screen size is " + outSize.x + " x " + outSize.y);
    } 
    catch (NoSuchMethodException ex) 
    {
      // This is the failure I expect when the deprecated APIs are not available
      Log.d(mTAG, "getSize not available - NoSuchMethodException");
    }  
    catch (InvocationTargetException e) 
    {
      Log.w(mTAG, "getSize not available - InvocationTargetException");
    } 
    catch (IllegalArgumentException e) 
    {
      Log.w(mTAG, "getSize not available - IllegalArgumentException");
    } 
    catch (IllegalAccessException e) 
    {
      Log.w(mTAG, "getSize not available - IllegalAccessException");
    }

    if (!sizeFound)
    {
      Log.i(mTAG, "Used deprecated methods as getSize not available");
      outSize = new Point(xiDisplay.getWidth(), xiDisplay.getHeight());
    }

    return outSize;
  }
Dan J
  • 25,433
  • 17
  • 100
  • 173
3

According to https://stackoverflow.com/a/1016941/2914140:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Community
  • 1
  • 1
CoolMind
  • 26,736
  • 15
  • 188
  • 224
1

What is wrong with Display's new function, getSize()? It'd be really easy to turn the Point object into the width and height values you need.

Turnsole
  • 3,422
  • 5
  • 30
  • 52
  • 2
    Isnt the getSize() method only avaiable from api lvl 13 and upwards or am I mistaken ? – Ukjent May 18 '12 at 21:47
  • We can use widthPixels to get information for: "The absolute width of the display in pixels." https://stackoverflow.com/a/37391867/1815624 – CrandellWS Sep 27 '18 at 01:27