Reputation: 3681
I want to use the function activity.getWindowManager().getDefaultDisplay().getwidth() but there is a warning that says this function is deprecated
What should I do ? Should I use this function anyway? or there is some other functions that do the same ?
Upvotes: 13
Views: 12283
Reputation: 24988
Try:
WindowManager windowmanager = (WindowManager) this.getContext()
.getSystemService(Context.WINDOW_SERVICE);
with:
Display display = windowmanager.getDefaultDisplay();
Point size = new Point();
try {
display.getRealSize(size);
} catch (NoSuchMethodError err) {
display.getSize(size);
}
int width = size.x;
int height = size.y;
or with:
DisplayMetrics displayMetrics = new DisplayMetrics();
windowmanager.getDefaultDisplay().getMetrics(displayMetrics);
int deviceWidth = displayMetrics.widthPixels;
int deviceHeight = displayMetrics.heightPixels;
Upvotes: 1
Reputation: 95998
A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code.
See this and this and this and this and this and so on............
Upvotes: 1
Reputation: 1753
Put your cursor on method's name and press F2
to get informations about latest API. (assuming you're using Eclipse)
Upvotes: 1
Reputation: 360
The correct thing to do is to check the SDK version. Depending on that you can use the deprecated function or do it using Point. See the following: Is it safe to use .getWidth on Display even though its deprecated.
Upvotes: 0
Reputation: 4119
Deprecated means that it shouldn't be used, but it's still there for compability reasons.
You should use instead:
Point size = new Point();
activity.getWindowManager().getDefaultDisplay().getSize(size);
int width = size.x;
int height = size.y;
Upvotes: 28
Reputation: 18633
From the Display
API reference:
int getWidth() This method was deprecated in API level 13. Use getSize(Point) instead.
Which means you'll instantiate a Point
, pass it to getSize()
and retrieve the x
from it.
Upvotes: 2
Reputation: 1926
Deprecated functions are those function of which new better alternates have been introduced and in future they might not be supported in new API's. But feel free to use them as it takes a lot of time for them to get expired.
Upvotes: 1