Android: Temporarily disable orientation changes in an Activity Android: Temporarily disable orientation changes in an Activity android android

Android: Temporarily disable orientation changes in an Activity


As explained by Chris in his self-answer, calling

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

and then

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

really works like charm... on real devices !

Don't think that it's broken when testing on the emulator, the ctrl+F11 shortcut ALWAYS change the screen orientation, without emulating sensors moves.

EDIT: this was not the best possible answer. As explained in the comments, there are issues with this method. The real answer is here.


None of the other answers did the trick perfectly for me, but here's what I found that does.

Lock orientation to current...

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);} else setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

When changing orientation should be allowed again, set back to default...

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);


Here is a more complete and up to date solution that works for API 8+, works for reverse portrait and landscape, and works on a Galaxy tab where the "natural" orientation is landscape (call activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) to unlock the orientation):

@SuppressWarnings("deprecation")@SuppressLint("NewApi")public static void lockActivityOrientation(Activity activity) {    Display display = activity.getWindowManager().getDefaultDisplay();    int rotation = display.getRotation();    int height;    int width;    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {        height = display.getHeight();        width = display.getWidth();    } else {        Point size = new Point();        display.getSize(size);        height = size.y;        width = size.x;    }    switch (rotation) {    case Surface.ROTATION_90:        if (width > height)            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);        else            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);        break;    case Surface.ROTATION_180:        if (height > width)            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);        else            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);        break;              case Surface.ROTATION_270:        if (width > height)            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);        else            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);        break;    default :        if (height > width)            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);        else            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);    }}