How do I get the CURRENT orientation (ActivityInfo.SCREEN_ORIENTATION_*) of an Android device? How do I get the CURRENT orientation (ActivityInfo.SCREEN_ORIENTATION_*) of an Android device? android android

How do I get the CURRENT orientation (ActivityInfo.SCREEN_ORIENTATION_*) of an Android device?


I ended up using the following solution:

private int getScreenOrientation() {    int rotation = getWindowManager().getDefaultDisplay().getRotation();    DisplayMetrics dm = new DisplayMetrics();    getWindowManager().getDefaultDisplay().getMetrics(dm);    int width = dm.widthPixels;    int height = dm.heightPixels;    int orientation;    // if the device's natural orientation is portrait:    if ((rotation == Surface.ROTATION_0            || rotation == Surface.ROTATION_180) && height > width ||        (rotation == Surface.ROTATION_90            || rotation == Surface.ROTATION_270) && width > height) {        switch(rotation) {            case Surface.ROTATION_0:                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;                break;            case Surface.ROTATION_90:                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;                break;            case Surface.ROTATION_180:                orientation =                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;                break;            case Surface.ROTATION_270:                orientation =                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;                break;            default:                Log.e(TAG, "Unknown screen orientation. Defaulting to " +                        "portrait.");                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;                break;                      }    }    // if the device's natural orientation is landscape or if the device    // is square:    else {        switch(rotation) {            case Surface.ROTATION_0:                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;                break;            case Surface.ROTATION_90:                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;                break;            case Surface.ROTATION_180:                orientation =                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;                break;            case Surface.ROTATION_270:                orientation =                    ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;                break;            default:                Log.e(TAG, "Unknown screen orientation. Defaulting to " +                        "landscape.");                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;                break;                      }    }    return orientation;}

NOTE: Some users (Geltrude and holtaf in the comments below) pointed out that this solution will not work on all devices as the direction of rotation from the natural orientation is not standardized.


Simple approach would be to use

getResources().getConfiguration().orientation

1 is for Potrait and 2 for Landscape.


public static int getScreenOrientation(Activity activity) {        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();        int orientation = activity.getResources().getConfiguration().orientation;        if (orientation == Configuration.ORIENTATION_PORTRAIT) {          if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270) {            return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;          } else {            return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;          }        }        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {          if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {            return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;          } else {            return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;          }        }        return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;      }