How to use @ ActivityInfo.ScreenOrientation

I am trying to create a method that returns me a serialization-dependent dependency on if the device is portable or tablet.

public int getScreenOrientation(boolean isTablet){ if(isTablet){ return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; } else { return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; } } 

But when I use setRequestedOrientation(getScreenOrientation)); , I get lint-error Must be one of: ActivityInfo.SCREEN_ORIENTATION_......... which I can suppress, but it does not look like clean code.

So, I found that getRequestedOrientation uses the @ActivityInfo.ScreenOrientation annotation. So I tried using it myself:

 @ActivityInfo.ScreenOrientation public int getScreenOrientation(boolean isTablet){ . . . } 

But the IDE gives me a message stating that Annotation @ActivityInfo.ScreenOrientation could not be found. But it is declared public in the official android source.

+7
java android annotations
source share
2 answers

Put the following comment on the annoying statement where the magic check starts for the @IntDef and @StringDef annotations , for example:

 //noinspection ResourceType setRequestOrientation(lock); 
+9
source share

Try @IntegerRes annotation @IntegerRes . This should work for you, since you are returning the integer attribute of the resource from android.R.attr.

https://developer.android.com/reference/android/support/annotation/IntegerRes.html http://developer.android.com/reference/android/R.attr.html#screenOrientation

The following is an example of working without IDE errors or warnings.

 @IntegerRes public static int getScreenOrientationPref() { if(sharedPreferences.getBoolean("LockOrientation", true)) { int orientation = sharedPreferences.getInt("Orientation", Configuration.ORIENTATION_LANDSCAPE); if(orientation == Configuration.ORIENTATION_LANDSCAPE) { return ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE; } else { return ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT; } } return ActivityInfo.SCREEN_ORIENTATION_USER; } 
0
source share

All Articles