Question

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.

Was it helpful?

Solution

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);
                }

OTHER TIPS

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();
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top