13

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 ?

Navid777
  • 3,591
  • 8
  • 41
  • 69
  • deprecated means it's old and it is still supported but it has been replaced by a new, better function, and is likely to be replaced in future editions of the library. you can use it if you want but it's best to earch for the function it has been replaced with. – Ionut Hulub Feb 25 '13 at 12:57
  • 7
    did you do any kind of search at all before asking the question ? – njzk2 Feb 25 '13 at 12:57
  • 1
    http://stackoverflow.com/questions/8111774/deprecated-meaning – zch Feb 25 '13 at 12:58
  • 3
    I don't think we need eight answers to a question which has a billion answers when Googling. Vote to close please – HXCaine Feb 25 '13 at 13:01
  • 4
    Why downvote, I dont get it all, the question came from lack of knowledge, if this QA site only for people who knows anything, then close this site, I vote this question up – Melvin Mar 24 '15 at 03:56

7 Answers7

28

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;
Michał Z.
  • 4,119
  • 1
  • 22
  • 32
2

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.

Vlad
  • 18,195
  • 4
  • 41
  • 71
1

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.

Fahad Ishaque
  • 1,916
  • 1
  • 17
  • 21
  • It is not exactly best practice to use deprecated functions, so I would not encourage the OP to use them – QkuCeHBH Feb 25 '13 at 13:07
  • That's only according to the docs. I have dozens of apps running on numerous deprecated methods. According to my experience this deprecation thing is just a warning of expiry so that devs could be encouraged for new developments. For reference take a look at changes Android have been through in thread pool exe. You migth agree with me after this. – Fahad Ishaque Feb 25 '13 at 13:20
1

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............

Community
  • 1
  • 1
Maroun
  • 94,125
  • 30
  • 188
  • 241
1

Put your cursor on method's name and press F2 to get informations about latest API. (assuming you're using Eclipse)

Piotr
  • 1,743
  • 1
  • 26
  • 40
1

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;
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
0

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.

Community
  • 1
  • 1
rdrobinson3
  • 360
  • 1
  • 4