Search code examples
androidandroid-orientation

Way to find device orientation change after calling `setRequestedOrientation`


Is there any way in Android to know device orientation change after we call setRequestedOrientation, I have tried following ways.

  1. getResources().getConfiguration().orientation
  2. OrientationEventListener
  3. Display getOrient = getWindowManager().getDefaultDisplay().getRotation();

But all the above methods are give same values even after changing the device orientation.
Can someone please suggest some way by which we can find the device orientation change after we call setRequestedOrientation.


Solution

  • Might be a workaround: Handle onOrientationChanged degree from OrientationEventListener added to currente context. For example:

    final OrientationEventListener orientationEventListener = new OrientationEventListener( getApplicationContext() ) {
    
        @Override
        public void onOrientationChanged( final int orientation ) {
            Log.i( "orientation_test", "orientation: " + orientation );
        }
    };
    
    orientationEventListener.enable();
    

    Add and enable it at onResume and disable at onPause of Activity for example.