Question

I try to use constructor of AlertDialog.Builder which gets a context and a theme:

AlertDialog.Builder b = new AlertDialog.Builder(this, 0);

It shows an error on my Eclipse (marking the "Builder" type): "Call requires API level 11 (current min is 7)".

When I use the constructor which gets only the context (without theme), it doesn't show this error.

  1. I want to be able to deploy my app also on old androids (such as version 2.1 etc.). Is there any convenient way for me to use the simple constructor of AlertDialog.Builder when it's an old version, and to use the more complicated constructor when it's API 11 and above?

  2. Just out of curiosity, how does the compiler know what API version is needed for the use of a certain method?

Was it helpful?

Solution

  1. You may use the const Build.VERSION.SDK_INT on an if clause, to check if your API level is above or below the required level (You will probably have to add an annotation to suppress the API level warning / error from that method / class).

  2. Android Lint does that for you. It checks which methods and ctors are allowed on a certain API level.

Example:

 if(Build.VERSION.SDK_INT >= 11) { 
   //API level 11 and above ctor here 
} else { 
  //Lower than API level 11 code here 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top