How to only execute code on certain API level

android

Solution

I'm not sure if this is going to solve your issue,

but what you are using to check version is not working under API 9 (and you are supporting since API 8).

You should use:

if (Build.VERSION.SDK_INT > 9) { 

Or as problematic function is API 11, check for "SDK_INT>10"

Then for lint errors on eclipse, do as people comment, disable lint errors or add the @SuppressLint("NewAPi") or the target to that function to 11.

Problem

For instance, this code: ``` if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD) { myCalendarView.setOnDateChangeListener( new OnDateChangeListener() { @Override public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) { Toast.makeText ( getApplicationContext(), ""+dayOfMonth, 0 ).show(); } } ); } ``` Gives error: Description Resource Path Location Type Call requires API level 11 (current min is 8): android.widget.CalendarView#setOnDateChangeListener example.java /example/src/com/example/example line 20 Android Lint Problem I understand why I get this error compile-time. But is there any way to mark a source Java class to only be used on certain API level-11? Or surround code blocks with a define/similar so the code is late-bound/jitted only on devices above API level-11? What is the best solution to achieve what I want? (Which is to provide an activity with CalendarView on devices capabile of it.)

Original source