Question

I saw in a android video from google developers that they implemented two methods and defined via annotation what method should be called based on the api level.

Unfortunatelly, I can`t find the video anymore(I can't remember which video was), so I searched StackOverflow for this question and found this: Different Java methods for different API Levels

In the question he used the annotation @apilevel and I can't find that annotation.

Basically, what I want to do is this:

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private int getDisplayWidth(Display display){
    Point size = new Point();
    display.getSize(size);
    return size.x;
}

@TargetApi(Build.VERSION_CODES.FROYO)
private int getDisplayWidth(Display display){
    return display.getWidth();
}

But ADT is giving me duplicated method error.

Était-ce utile?

La solution

Use:

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private int getDisplayWidth(Display display){
    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB_MR2) {
      Point size = new Point();
      display.getSize(size);
      return size.x;
    }

    return display.getWidth();
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top