Get rotation from its “natural” orientation on your android device

  Uncategorized

Up to API Level 7 you can use android.view.Display.getOrientation() to get the rotation of the screen from its default orientation. But this method was deprecated in API level 8.

But now you can use

getWindowManager().getDefaultDisplay().getRotation(‌​);

The returned value is:

  • Surface.ROTATION_0 (no rotation)
  • Surface.ROTATION_90
  • Surface.ROTATION_180
  • Surface.ROTATION_270
  • If your device has a naturally portrait screen, and the user has turned it on its side to go into a landscape orientation, the value returned here may be either Surface.ROTATION_90 or Surface.ROTATION_270 depending on the direction it was turned.

    With this simple line of code you can get the information about the screen orientation related to the default. This could be helpful, if you want to deal with your Sensor.