title: Android: get device orientation from Azimuth, roll & pitch
body: <p>I need to get the updated device orientation, but I have to fix my activity to Portrait (which I did in the layout xml file), which prevents me from using this:</p>
  
  <pre><code>int rotation = getWindowManager().getDefaultDisplay().getRotation();
  </code></pre>
  
  <p>because it always gives me the portrait rotation,</p>
  
  <p>So, I'm trying to rely on the sensors. I found that <code>Sensor.TYPE_ORIENTATION</code> is deprecated, so I'm using a combination of <code>Sensor.TYPE_ACCELEROMETER</code> &amp; <code>Sensor.TYPE_MAGNETIC_FIELD</code> and here is the event listener:</p>
  
  <pre><code>SensorEventListener sensorEventListener = new SensorEventListener() {
      float[] mGravity;
      float[] mGeomagnetic;
  
      @Override
      public void onSensorChanged(SensorEvent event) {
          if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
              mGravity = event.values;
          if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
              mGeomagnetic = event.values;
          if (mGravity != null &amp;&amp; mGeomagnetic != null) {
              float R[] = new float[9];
              float I[] = new float[9];
              boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
              if (success) {
                  float orientationData[] = new float[3];
                  SensorManager.getOrientation(R, orientationData);
                  azimuth = orientationData[0];
                  pitch = orientationData[1];
                  roll = orientationData[2];
                  // now how to use previous 3 values to calculate orientation
              }
          }
      }
  
      @Override
      public void onAccuracyChanged(Sensor sensor, int accuracy) {
          // TODO Auto-generated method stub
  
      }
  };
  </code></pre>
  
  <p>Now <strong>Question is</strong>, how to use the 3 values <code>azimuth</code>, <code>pitch</code> &amp; <code>roll</code> to detect current device orientation as one of these:</p>
  
  <ul>
  <li>Landscape (rotation 0)</li>
  <li>Portrait (rotation 90)</li>
  <li>Reverse Landscape (rotation 180)</li>
  <li>Reverse Portrait (rotation 270)</li>
  </ul>