title: Screen orientation lock
body: <p>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. </p>
  
  <p>Is there any way to reliably map rotation to orientation?</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>EDIT: This code is meant to get the current orientation and lock it. The orientation is locked temporarily, and then released to the user.</p>