Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android deprecate warning shows even though I specify api level

I am coding to api level 11. So to get the width of the display I have the following function

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        Point size = new Point();
        display.getSize(size);
        width = size.x;
    }
    else {
        width = display.getWidth();
    }

I do all this so I would not get the deprecate warning. But I still get the warning for getWidth. Why does the warning not go away?

I also get an error for getSize saying call requires api level 13 but 11 is detected.

like image 876
learner Avatar asked Jan 05 '14 16:01

learner


2 Answers

The IDE isn't quite intelligent enough to realise what you are trying to do here, hence why you are still getting warnings.

If you were hellbent on getting rid of them, you could define two methods: one which executes the code in the Honeycomb route, and has the annotation @TargetApi(13) and the second which executes the code in the other route with the annotation @SuppressWarnings("deprecation"). Personally, I wouldn't worry.

Example:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
    width = getWidthHoneycombMR2();
}
else {
    width = getWidthLegacy(display);
}

...

@TargetApi(13)
public int getWidthHoneycombMR2() {
    Point size = new Point();
    display.getSize(size);
    return size.x;
}

@SuppressWarnings("deprecation")
public int getWidthLegacy(Display display) {
    return display.getWidth();
}
like image 146
Alex Curran Avatar answered Nov 02 '22 00:11

Alex Curran


depracation basically means that the code will stop being supported soon because the developer feels that there is a better way to do things. in this case use Display Metrics

DisplayMetrics display = this.getResources().getDisplayMetrics();

    int width = display.widthPixels;
    int height = display.heightPixels;
like image 32
Or Bar Avatar answered Nov 02 '22 00:11

Or Bar