Reputation: 10711
I'm trying to target as many architectures as possible, as well as having few warnings as possible.
Point dims = new Point();
if (android.os.Build.VERSION.SDK_INT >= 13) {
mWindowManager.getDefaultDisplay().getSize(dims);
} else if (android.os.Build.VERSION.SDK_INT < 13) {
dims.x = mWindowManager.getDefaultDisplay().getWidth();
dims.y = mWindowManager.getDefaultDisplay().getHeight();
}
But this gives me the error and warning:
Call requires API level 13 (current min is 10): android.view.Display#getSize
The method getWidth() from the type Display is deprecated
They say to change the manifesto (or here) but why is this above not working for the compiler? Can't I get rid of both with the range of apis 10 to 18 ?
Upvotes: 3
Views: 2304
Reputation: 12949
To retrieve the display size, the recommended way is to use
context.getResources().getDisplayMetrics()
instead. Then use the widthPixels and heightPixels properties.
Using the current context gives you the dimensions of the current display in case of multiple displays, which is also safer.
Upvotes: 0
Reputation: 152867
You already have the SDK version checking in place so the deprecation warning from compiler and lint warning about new API can be ignored.
Pull the code snippet to a separate method and add the following annotations to it:
@TargetApi(13)
@SuppressWarnings("deprecation")
Note that the if
part of your else if
is redundant. Plain else
is enough.
Upvotes: 7