0

Is it possible to get a event when the screen orientation changes while

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

is set?

The purpose of this is, once a user opens a video in full screen mode, then the screen will go landscape.

When the user goes (back) to portrait, the video minimizes.

InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
Roy
  • 656
  • 12
  • 28

2 Answers2

3

This method of activity will called.

 @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }
    }
Hradesh Kumar
  • 1,765
  • 15
  • 20
  • Hmm, I doesn't get called anymore once I set the orientation. – Roy Feb 25 '14 at 10:54
  • I am also interested to get ALL 4 orientations and Did not find a suitable solution yet. I started implementing Sensors. But this includes all orientations of the device. Would like to know, how I only can receive 0,90,180,270. – icbytes Feb 25 '14 at 10:58
  • @icbytes, look at this question http://stackoverflow.com/questions/3611457/android-temporarily-disable-orientation-changes-in-an-activity?rq=1 – Roy Feb 25 '14 at 11:00
  • 1
    Please share your code. I think you are using it in oncreate method. – Hradesh Kumar Feb 25 '14 at 11:07
  • 1
    Well, I don't know if you tried it yourself but once you set the requested orientation (to landscape or portrait), the onConfigurationChanged will no longer be called. Thanks – Roy Feb 25 '14 at 11:10
  • Add, android:configChanges="keyboardHidden|orientation|screenSize" to your activity tag in manifest.xml – Hradesh Kumar Feb 25 '14 at 11:14
1

Try this:

private OrientationEventListener mOrientationEventListener;
private int mOrientation =  -1;
private static final int ORIENTATION_PORTRAIT_NORMAL =  1;
private static final int ORIENTATION_PORTRAIT_INVERTED =  2;
private static final int ORIENTATION_LANDSCAPE_NORMAL =  3;
private static final int ORIENTATION_LANDSCAPE_INVERTED =  4;

 @Override
 protected void onResume() {
                 super.onResume();
                 if (mOrientationEventListener == null) {
                     mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {

                         @Override
                         public void onOrientationChanged(int orientation) {

                         // determine our orientation based on sensor response
                         int lastOrientation = mOrientation;
                         if (orientation >= 315 || orientation < 45) {
                             if (mOrientation != ORIENTATION_PORTRAIT_NORMAL) {
                                 mOrientation = ORIENTATION_PORTRAIT_NORMAL;
                                 }
                             }
                         else if (orientation < 315 && orientation >= 225) {
                             if (mOrientation != ORIENTATION_LANDSCAPE_NORMAL) {
                                 mOrientation = ORIENTATION_LANDSCAPE_NORMAL;
                                 }
                             }
                         else if (orientation < 225 && orientation >= 135) {
                             if (mOrientation != ORIENTATION_PORTRAIT_INVERTED) {
                                 mOrientation = ORIENTATION_PORTRAIT_INVERTED;
                                 }                                        }
                         else {
                             // orientation <135 && orientation > 45
                             if (mOrientation != ORIENTATION_LANDSCAPE_INVERTED) {
                                 mOrientation = ORIENTATION_LANDSCAPE_INVERTED;
                                 }                                        }
                         if (lastOrientation != mOrientation) {
                        //   changeRotation(mOrientation, lastOrientation);
                             }
                         }
                         };
                             }

                 if (mOrientationEventListener.canDetectOrientation()) {
                     mOrientationEventListener.enable();
                     }
                 }

             public void onOrientationChanged(int orientation) {

                 // determine our orientation based on sensor response
                 int lastOrientation = mOrientation;
                 Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
                 if (display.getOrientation() == Surface.ROTATION_0) {
                     // landscape oriented devices
                     if (orientation >= 315 || orientation < 45) {
                         if (mOrientation != ORIENTATION_LANDSCAPE_NORMAL) {
                             mOrientation = ORIENTATION_LANDSCAPE_NORMAL;
                             }         }
                     else if (orientation < 315 && orientation >= 225) {
                         if (mOrientation != ORIENTATION_PORTRAIT_INVERTED) {
                             mOrientation = ORIENTATION_PORTRAIT_INVERTED;
                             }
                         } else if (orientation < 225 && orientation >= 135) {
                             if (mOrientation != ORIENTATION_LANDSCAPE_INVERTED) {
                                 mOrientation = ORIENTATION_LANDSCAPE_INVERTED;
                                 }
                             } else if (orientation <135 && orientation > 45) {
                                 if (mOrientation != ORIENTATION_PORTRAIT_NORMAL) {
                                     mOrientation = ORIENTATION_PORTRAIT_NORMAL;
                                     }                                }
                     } else {
                         // portrait oriented devices
                         if (orientation >= 315 || orientation < 45) {
                             if (mOrientation != ORIENTATION_PORTRAIT_NORMAL) {
                                 mOrientation = ORIENTATION_PORTRAIT_NORMAL;
                                 }
                             } else if (orientation < 315 && orientation >= 225) {
                                 if (mOrientation != ORIENTATION_LANDSCAPE_NORMAL) {
                                     mOrientation = ORIENTATION_LANDSCAPE_NORMAL;
                                     }
                                 } else if (orientation < 225 && orientation >= 135) {
                                     if (mOrientation != ORIENTATION_PORTRAIT_INVERTED) {
                                         mOrientation = ORIENTATION_PORTRAIT_INVERTED;
                                         }
                                     } else if (orientation <135 && orientation > 45) {

                                         if (mOrientation != ORIENTATION_LANDSCAPE_INVERTED) {
                                             mOrientation = ORIENTATION_LANDSCAPE_INVERTED;
                                             }
                                         }     }
                 if (lastOrientation != mOrientation) {

                //   changeRotation(mOrientation, lastOrientation);     } }

                 }
             }

             @Override
                    public void onConfigurationChanged(Configuration newConfig) {
                        super.onConfigurationChanged(newConfig);
                }
snytek
  • 343
  • 1
  • 9