getSize() not supported on older Android OS versions, getWidth() / getHeight() deprecated

11,104

Solution 1

The best (and by best, I mean the option that would work pretty much every time) option would be to use reflection. Check out the Android Backwards Compatibility Backwards Compatibility guidelines (updated with new location of the article on reflection).

While tyczj's answer will work perfectly so long as the deprecated functions are still in the SDK, as soon as they are removed you will have no way of using them or running your app on an older device if you still want to build against the latest SDK.

Reflection solves this problem by effectively dynamically detecting the function at runtime, which means that even if you build against ICS, so long as the minSdkVersion is correct, you can have your app run on a device with Gingerbread, or Froyo for example.

Solution 2

i have two functions, sending the context and gettin height and width in pixels.

public static int getWidth(Context mContext){
    int width=0;
    WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    if(Build.VERSION.SDK_INT>Build.VERSION_CODES.HONEYCOMB){                   
        Point size = new Point();
        display.getSize(size);
        width = size.x;
    }
    else{
        width = display.getWidth();  // deprecated
    }
    return width;
}

and

public static int getHeight(Context mContext){
    int height=0;
    WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    if(Build.VERSION.SDK_INT>Build.VERSION_CODES.HONEYCOMB){               
        Point size = new Point();
        display.getSize(size);
        height = size.y;
    }else{          
        height = display.getHeight();  // deprecated
    }
    return height;      
}

Solution 3

you can do something like this

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){
           //do stuff pertaining to this version here
}else{
           //other versions
}

Solution 4

I think sort of thing RivieraKid is suggesting, would be something like this:

static Point getDisplaySize(Display d)
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    {
        return getDisplaySizeGE11(d);
    }
    return getDisplaySizeLT11(d);
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
static Point getDisplaySizeGE11(Display d)
{
    Point p = new Point(0, 0);
    d.getSize(p);
    return p;
}
static Point getDisplaySizeLT11(Display d)
{
    try
    {
        Method getWidth = Display.class.getMethod("getWidth", new Class[] {});
        Method getHeight = Display.class.getMethod("getHeight", new Class[] {});
        return new Point(((Integer) getWidth.invoke(d, (Object[]) null)).intValue(), ((Integer) getHeight.invoke(d, (Object[]) null)).intValue());
    }
    catch (NoSuchMethodException e2) // None of these exceptions should ever occur.
    {
        return new Point(-1, -1);
    }
    catch (IllegalArgumentException e2)
    {
        return new Point(-2, -2);
    }
    catch (IllegalAccessException e2)
    {
        return new Point(-3, -3);
    }
    catch (InvocationTargetException e2)
    {
        return new Point(-4, -4);
    }
}
Share:
11,104
karl_
Author by

karl_

Updated on July 29, 2022

Comments

  • karl_
    karl_ almost 2 years

    So how do I write code to accommodate for this? I don't want to leave deprecated API calls in my code, but I also don't want to lose the users with (slightly) older devices. Is there some sort of compatibility setting I can implement?

    Rel. code

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

    vs. the older method:

    int screen_width = getWindowManager().getDefaultDisplay().getWidth();
    int screen_height = getWindowManager().getDefaultDisplay().getHeight();
    
  • RivieraKid
    RivieraKid about 12 years
    Unfortunately, this will only work for so long as the old methods are in the build SDK. @karl_, you don't explicitly state exactly how old a device you want to support, but using reflection as in my answer will allow you to support any Android version regardless of the SDK you are building against.
  • karl_
    karl_ about 12 years
    I hadn't seen that part of the developer documents yet- good answer.
  • RivieraKid
    RivieraKid about 12 years
    It's pretty obscure if you don't know what to look for, I stumbled on it totally by accident.
  • EGHDK
    EGHDK about 11 years
    So what is reflection. I believe that link is now dead.
  • james82345
    james82345 about 11 years
    The link developer.android.com/resources/articles/… is no longer active.