1

On iOS, device orientation can be different from interface orientation, so we have two different enums. I'm not necessarily looking for enums. Does Android make the same distinction? I want to detect interface orientation changes rather than device.

People seem to be using onConfigurationChanged() to detect orientation changes, but it wasn't clear to me exactly what kind of orientation change this is.

Community
  • 1
  • 1
Mark13426
  • 2,569
  • 6
  • 41
  • 75

1 Answers1

0

this methods can detect (and) set orientation:

getResources().getConfiguration().orientation and setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

I'm using those methods to catch all orientations:

inside onCreate:

initOrientation();

where

private void initOrientation() {
        mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        mLastRotation = mWindowManager.getDefaultDisplay().getRotation();
        myOrientationEventListener
        = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_UI){
         @Override
         public void onOrientationChanged(int orientation) {
//         Log.d(TAG, "ORIENTATION: " + orientation);
         Display display = mWindowManager.getDefaultDisplay();
         int rotation = display.getRotation();
         if (((rotation == Surface.ROTATION_90 && mLastRotation == Surface.ROTATION_270)
                 || (rotation == Surface.ROTATION_270 && mLastRotation == Surface.ROTATION_90)
                 || (rotation == Surface.ROTATION_0 && mLastRotation == Surface.ROTATION_180)
                 || (rotation == Surface.ROTATION_180 && mLastRotation == Surface.ROTATION_0))) {
             //if ((rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && rotation != mLastRotation) {
             Log.i(TAG, "changed >>> " + rotation + " :: " + _.mWidth);

             // do something

             mLastRotation = rotation;
         }
         }
         };

           if (myOrientationEventListener.canDetectOrientation()){
            Toast.makeText(this, "Can DetectOrientation", Toast.LENGTH_LONG).show();
            myOrientationEventListener.enable();
           }
           else{
            Toast.makeText(this, "Can't DetectOrientation", Toast.LENGTH_LONG).show();
          //  finish();
           }
    }

it depends on what your really want to realize. the method above can detect portrait>portrait and landscape>landscape shifts.

In order to force lock and release rotation I'm using this approach:

protected void mLockScreenRotation(int i)
{
  // Stop the screen orientation changing during an event
    switch (i)
    {
    default:
    case 0:
        switch (this.getResources().getConfiguration().orientation)
        {
          case Configuration.ORIENTATION_PORTRAIT:
              this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
              break;
          case Configuration.ORIENTATION_LANDSCAPE:
              this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
              break;
        }
    case 1:
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        break;
    case 2:
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        break;
    }
}

mLockScreenRotation(0) - locks

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • This is an interesting approach with the sensor manager. Do you get rotation updates if the screen is locked or your app is paused/backgrounded? – Mark13426 Oct 26 '16 at 18:58
  • @Mark13426 , I do not say about the activity,I'm interesting too how it works in the backgroud. but you can use this code inside a foreground service that should work. – Vyacheslav Oct 26 '16 at 19:04
  • @Mark13426 , I've edited my post and have written one more method. – Vyacheslav Oct 26 '16 at 19:04
  • It seems Android doc suggests SENSOR_DELAY_NORMAL for orientation changes: https://developer.android.com/reference/android/hardware/SensorManager.html#SENSOR_DELAY_NORMAL. Have you tried it in your code? – Mark13426 Oct 26 '16 at 19:09
  • 1
    @Mark13426 , this is just a precision for detection. You can use any value you want. – Vyacheslav Oct 26 '16 at 19:15