Skip to content

Instantly share code, notes, and snippets.

@peanutwolf
Created October 6, 2015 08:08
Show Gist options
  • Save peanutwolf/2287c46aa796c38f22e5 to your computer and use it in GitHub Desktop.
Save peanutwolf/2287c46aa796c38f22e5 to your computer and use it in GitHub Desktop.
Locking orientation on Android
private void lockOrientation(){
int orientation;
int rotation = ((WindowManager) getActivity().getSystemService(
Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
switch (rotation) {
case Surface.ROTATION_0:
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
break;
case Surface.ROTATION_90:
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
break;
case Surface.ROTATION_180:
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
break;
default:
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
break;
}
getActivity().setRequestedOrientation(orientation);
}
private void unlockOrientation(){
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment