3

I want develop a video player app, when the device is landscape, the video is full screen playing, when device is portrait the video is half-screen playig. In the landscape, there is a button, when user click it, app will force to portrait(At this moment, the devie still horizontal. What I want: When user change the device to vertical, and change device to horizontal again, the app can auto change from portrait to landscape.

I call the setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); to force to portrait. After I call setRequestedOrientation(),onConfigurationChanged() will NOT call again when I change the device from horizontal to vertical. So there is no time point to call setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR).

I try the OrientationEventListener, but in onOrientationChanged() there is a lot of value will return back. But what I want just the device Orientation(landscape or portrait).

Is there any other method to do what I want simply?

Nicole
  • 31
  • 1
  • possible duplicate of [how to detect orientation of android device?](http://stackoverflow.com/questions/5112118/how-to-detect-orientation-of-android-device) – aga Dec 23 '14 at 08:23

2 Answers2

4

You have to use OrientationEventListener, I use like this, a then use onConfigurationChanged like always:

 OrientationEventListener orientationEventListener = new OrientationEventListener(getApplicationContext()) {
            @Override
            public void onOrientationChanged(int orientation) {
                boolean isPortrait = isPortrait(orientation);
                if (!isPortrait && savedOrientation == ORIENTATTION_PORTRAIT) {
                    savedOrientation = ORIENTATION_LANDSACAPE;
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
                } else if (isPortrait && savedOrientation == ORIENTATION_LANDSACAPE) {
                    savedOrientation = ORIENTATTION_PORTRAIT;
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
                }
            }
        };
        orientationEventListener.enable();

private boolean isPortrait(int orientation) {
        if (orientation < 45 || orientation > 315) {
            return true;
        }
        return false;
    }

You have more info here: https://developer.android.com/reference/android/view/OrientationEventListener.html

-2

You can override onConfigurationChanged(Configuration config); :

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // Landscape
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        // Portrait
    }
}
Rogue
  • 751
  • 1
  • 17
  • 36