karl_
karl_

Reputation: 340

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

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();

Upvotes: 8

Views: 11840

Answers (5)

slott
slott

Reputation: 3335

I usually have a super class ie. BaseActivity with a generic method for getting a point with the current screen size. Keeps everything nice and clean in the actual activity.

/**
 * Return screen size as a point.
 * @return
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
@SuppressWarnings("deprecation")
protected Point getSize() {
    final Point point = new Point();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        getWindowManager().getDefaultDisplay().getSize(point);
    }
    else {
        final Display display = getWindowManager().getDefaultDisplay();
        point.x = display.getWidth();
        point.y = display.getHeight();
    }
    return point;
}

Upvotes: 0

RivieraKid
RivieraKid

Reputation: 5961

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.

Upvotes: 3

Steve Waring
Steve Waring

Reputation: 3033

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);
    }
}

Upvotes: 1

Jorgesys
Jorgesys

Reputation: 126533

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;      
}

Upvotes: 13

tyczj
tyczj

Reputation: 73946

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
}

Upvotes: 3

Related Questions