title: Detect screen orientation in Android 2.2+
body: <p>I have some problem with screen orientation. Basically what I am trying to do is to determine exact screen orientation (including reverse landscape and reverse portrait). I wrote method below to do it for me and it seems to work perfectly on my Transformer however when I start same code on emulator some things are messed.</p>
  
  <p>Basically what is happening is that whenever in emulator I switch to landscape (ROTATION_90) according to documentation it is supposed to be reverse landscape (and for example on Transformer with ROTATION_90 device is in reverse portrait as Android documentation describes). Unfortunately on emulator for some reason it seems to be normal landscape mode.</p>
  
  <p>So my question is this code below correct ir problem lies in it. Or perhaps I am not taking into account some other parameters (like emulator has barely accelerometer enabled for instance).</p>
  
  <pre><code>public static int getDeviceCurrentOrientation() {
  
      WindowManager lWindowManager = (WindowManager) getContext()
              .getSystemService(Activity.WINDOW_SERVICE);
  
      Configuration cfg = getContext().getResources().getConfiguration();
      int lRotation = lWindowManager.getDefaultDisplay().getRotation();
  
      int rotation1;
      int orientation;
  
      rotation1 = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
      if (cfg.orientation == Configuration.ORIENTATION_LANDSCAPE)
          rotation1 = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
      else if (cfg.orientation == Configuration.ORIENTATION_PORTRAIT)
          rotation1 = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
      else
          rotation1 = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
  
      switch (lRotation) {
      case Surface.ROTATION_0:
          orientation = 0;
          break;
      case Surface.ROTATION_90:
          orientation = 1;
          break;
      case Surface.ROTATION_180:
          orientation = 2;
          break;
      case Surface.ROTATION_270:
          orientation = 3;
          break;
      default:
          orientation = -1;
      }
  
      if ((orientation == 0)
              &amp;&amp; (rotation1 == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE))
          return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
  
      if ((orientation == 0)
              &amp;&amp; (rotation1 == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT))
          return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
  
      if ((orientation == 1)
              &amp;&amp; (rotation1 == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT))
          return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
  
      if ((orientation == 1)
              &amp;&amp; (rotation1 == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE))
          return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
  
      if ((orientation == 2)
              &amp;&amp; (rotation1 == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT))
          return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
  
      if ((orientation == 2)
              &amp;&amp; (rotation1 == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE))
          return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
  
      if ((orientation == 3)
              &amp;&amp; (rotation1 == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT))
          return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
  
      if ((orientation == 3)
              &amp;&amp; (rotation1 == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE))
          return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
  
      return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
  }
  </code></pre>