Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Screen orientation lock

Is there a reliable way to lock screen orientation on all Android devices? The code below works for my Nexus S and other phones, but for some reason ROTATION_90 corresponds to SCREEN_ORIENTATION_REVERSE_PORTRAIT on the Xoom.

Is there any way to reliably map rotation to orientation?

private void lockScreenOrientation() {     if (!mScreenOrientationLocked) {         final int orientation = getResources().getConfiguration().orientation;         final int rotation = getWindowManager().getDefaultDisplay().getOrientation();          if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {             if (orientation == Configuration.ORIENTATION_PORTRAIT) {                 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);             }             else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {                 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);             }         }         else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) {             if (orientation == Configuration.ORIENTATION_PORTRAIT) {                 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);             }             else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {                 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);             }         }          mScreenOrientationLocked = true;     } }  private void unlockScreenOrientation() {     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);     mScreenOrientationLocked = false; } 

EDIT: This code is meant to get the current orientation and lock it. The orientation is locked temporarily, and then released to the user.

like image 546
Michael Pardo Avatar asked Jul 06 '11 16:07

Michael Pardo


People also ask

Where is screen orientation lock?

If you don't want the screen to switch between portrait and landscape when you move the device, you can lock the screen orientation. To do this, swipe down from the right side of the top panel. Hold the device in the orientation in which you want it locked. On the drop-down menu, touch the “Auto Rotate” button.

How do I fix orientation lock?

Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off.

How do I adjust screen orientation?

Select the Start button, then type settings. Select Settings > System > Display, and choose a screen orientation from the drop-down list next to Display orientation.


2 Answers

Here is my solution it works on phones and tablets in any Android SDK.

switch (getResources().getConfiguration().orientation){         case Configuration.ORIENTATION_PORTRAIT:             if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.FROYO){                 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);             } else {                 int rotation = getWindowManager().getDefaultDisplay().getRotation();                 if(rotation == android.view.Surface.ROTATION_90|| rotation == android.view.Surface.ROTATION_180){                     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);                 } else {                     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);                 }             }            break;          case Configuration.ORIENTATION_LANDSCAPE:             if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.FROYO){                 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);             } else {                 int rotation = getWindowManager().getDefaultDisplay().getRotation();                 if(rotation == android.view.Surface.ROTATION_0 || rotation == android.view.Surface.ROTATION_90){                     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);                 } else {                     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);                 }             }         break;     } 
like image 62
Mauricio Avatar answered Oct 08 '22 17:10

Mauricio


I modified diyism's answers slightly to compensate for the fact that you can't use reverse_landscape and reverse_portrait modes before version 2.3

private static void disableRotation(Activity activity) {            final int orientation = activity.getResources().getConfiguration().orientation;     final int rotation = activity.getWindowManager().getDefaultDisplay().getOrientation();      // Copied from Android docs, since we don't have these values in Froyo 2.2     int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8;     int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9;      if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO)     {         SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;         SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;     }      if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)     {         if (orientation == Configuration.ORIENTATION_PORTRAIT)         {             activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);         }         else if (orientation == Configuration.ORIENTATION_LANDSCAPE)         {             activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);         }     }     else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270)      {         if (orientation == Configuration.ORIENTATION_PORTRAIT)          {             activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);         }         else if (orientation == Configuration.ORIENTATION_LANDSCAPE)          {             activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);         }     } }  private static void enableRotation(Activity activity) {     activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); } 
like image 45
crobicha Avatar answered Oct 08 '22 16:10

crobicha