Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect screen rotation through 180 degrees from landscape to landscape orientation?

Tags:

I've seen a few other questions similar to this but no answers.

Rotating from portrait to landscape (either direction) and back again, we get the helpful call to onConfigurationChanged().

However, when rotating from landscape to landscape (through 180 degrees) onConfigurationChanged() is not called.

I've seen mention of using OrientationEventListener but this seems flakey to me because you can rotate quickly around without triggering a display orientation change.

I've tried adding a layout change listener, but with no success.

So the question is, how to reliably detect such a change in landscape orientation?

like image 490
Mark Avatar asked Mar 28 '12 13:03

Mark


People also ask

How can we detect the orientation of the screen?

You can detect this change in orientation on Android as well as iOS with the following code: var supportsOrientationChange = "onorientationchange" in window, orientationEvent = supportsOrientationChange ? "orientationchange" : "resize"; window.

How do I get my screen to rotate 180?

This feature can be enabled or disabled under 'Launcher' > 'Settings' > 'Display' > 'Auto-Rotate screen'. There are third-party apps in the Google Play Store which allow force and lock screen orientation.

How do I calibrate my screen rotation?

Find and turn on the "Auto-rotate" tile in the quick-setting panel. You can also go to Settings > Display > Auto-rotate screen to turn it on. Your phone screen should rotate automatically now if nothing is wrong with the sensors.

What is sensor landscape mode?

When set to landscape the screen is fixed but when set to sensor landscape it can rotate to any orientation.


1 Answers

OrientationEventlistener won't work when the device isn't rotating/moving.

I find display listener is a better way to detect the change.

     DisplayManager.DisplayListener mDisplayListener = new DisplayManager.DisplayListener() {
        @Override
        public void onDisplayAdded(int displayId) {
           android.util.Log.i(TAG, "Display #" + displayId + " added.");
        }

        @Override
        public void onDisplayChanged(int displayId) {
           android.util.Log.i(TAG, "Display #" + displayId + " changed.");
        }

        @Override
        public void onDisplayRemoved(int displayId) {
           android.util.Log.i(TAG, "Display #" + displayId + " removed.");
        }
     };
     DisplayManager displayManager = (DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE);
     displayManager.registerDisplayListener(mDisplayListener, UIThreadHandler);
like image 181
superuser Avatar answered Oct 25 '22 05:10

superuser