January 15, 2017

How to Get Current Screen Orientation in Android

By Lalit Vasan


Android devices have both landscape and portrait modes and can flawlessly move between these modes. The Android working framework naturally handles these progressions for your application. You can also detect in which orientation is your android device. The natural orientation of a device is the orientation in which the orientation is 0 on all the three axes. Below method returns the current screen orientation mode name of your android device.

public String getScreenOrientation(Context context){
        final int screenOrientation = ((WindowManager)  context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
        switch (screenOrientation) {
            case Surface.ROTATION_0:
                return "SCREEN_ORIENTATION_PORTRAIT";
            case Surface.ROTATION_90:
                return "SCREEN_ORIENTATION_LANDSCAPE";
            case Surface.ROTATION_180:
                return "SCREEN_ORIENTATION_REVERSE_PORTRAIT";
            default:
                return "SCREEN_ORIENTATION_REVERSE_LANDSCAPE";
        }
    }

Enjoy Coding and Share Knowledge