Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use .getWidth on Display even though its deprecated

So i have a small problem, i'm writing a function which need to send screen width to server. I got it all to work, and i use:

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

to get width. However .getWidht() function is deprecated and it says u need to use:

Point size = new Point();
display.getSize(size);

But that function is only avaible for api level 13 or more, and my minimum sdk is 8. So what can i do? Is it safe if i stay with getWidth? Why adding new function and not make them backward compatible?

like image 647
gabrjan Avatar asked Oct 08 '12 11:10

gabrjan


2 Answers

May be this approach will be helpful:

DisplayMetrics displaymetrics = new DisplayMetrics();
mContext.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenWidth = displaymetrics.widthPixels;
int screenHeight = displaymetrics.heightPixels;
like image 151
iBog Avatar answered Sep 20 '22 11:09

iBog


You can check for API level at runtime, and choose which to use, e.g.:

final int version = android.os.Build.VERSION.SDK_INT;
final int width;
if (version >= 13)
{
    Point size = new Point();
    display.getSize(size);
    width = size.x;
}
else
{
    Display display = getWindowManager().getDefaultDisplay(); 
    width = display.getWidth();
}
like image 30
nmw Avatar answered Sep 22 '22 11:09

nmw