7

I try to create a method which returns me the screenorientation dependend on wether the Device is a handheld or a 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 a lint-error Must be one of: ActivityInfo.SCREEN_ORIENTATION_......... which I can suppress, but that doesn't look like clean code.

So I found, that getRequestedOrientation uses the @ActivityInfo.ScreenOrientation Annotation. So I tried to use it myself:

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

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

1
  • 1
    I'm in the same situation and I really don't understand why I can't use @ActivityInfo.ScreemOrientation...
    – tbruyelle
    May 6, 2015 at 12:27

2 Answers 2

9

Put the following comment above the annoying statement where the magic constant inspection for @IntDef and @StringDef annotation is triggered, for example:

//noinspection ResourceType
setRequestOrientation(lock);
1
  • 1
    I am not the biggest fan of suppressing the error but it works and it's better than writing 5 Lines of Code in every Activity. Thank you! Apr 21, 2015 at 7:23
0

Try the annotation @IntegerRes instead. This should work fine for you since you are returning an integer resource attribute 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 example below is working for me 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;
}
1
  • 1
    Sorry, this does not work. The ScreenOrientationValues are final ints and not resources. So the IDE will throw an error at every line != return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE which is 0 Feb 19, 2015 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.