Home » Android » android – Toggling fullscreen & orientation like YouTube

android – Toggling fullscreen & orientation like YouTube

Posted by: admin June 15, 2020 Leave a comment


I am trying to mimic the behavior of the YouTube Android app when the “fullscreen” button is clicked in the video player:

  • If device is currently in portrait, immediately rotate to landscape (even if user is still holding the device in portrait) and remain in landscape until user rotates device to landscape and then rotates back to portrait
    • If device is currently in landscape, immediately rotate to portrait (Even if user is still holding the device in portrait) and remain in portrait until the user rotates the device to portrait and then rotates back to landscape.
    • At anytime, allow the user to manually rotate their device to the desired orientation.

It seems that if I force the rotation to landscape or portrait using:




… I immediately lose the ability to detect sensor orientation changes (i.e. once the user is in landscape, and they want to manually rotate the device back to portrait).

If I change the requested orientation to unspecified or sensor in onConfigurationChanged, the orientation briefly flips to landscape/portrait (whatever I requested from above) and then snaps back to the orientation that matches how the device is held.

Any thoughts on how to achieve my goals above?

How to&Answers:

I had the excact same problem. What I ended up with was using an OrientationListener to detect when the user had actually tilted the phone to landscape and then setting the orientation to SCREEN_ORIENTATION_SENSOR.

OrientationEventListener orientationEventListener = 
new OrientationEventListener(getActivity()) {
    public void onOrientationChanged(int orientation) {
        int epsilon = 10;
        int leftLandscape = 90;
        int rightLandscape = 270;
        if(epsilonCheck(orientation, leftLandscape, epsilon) ||
           epsilonCheck(orientation, rightLandscape, epsilon)){

        private boolean epsilonCheck(int a, int b, int epsilon) {
            return a > b - epsilon && a < b + epsilon;

Here is the documentation for OrientationEventListener : Documentation

You would also need to add checks for portrait , because you described needing that in your original post.


Big tnx to havch

It is gold, I was stuck on that for 5 hours. Here is my kotlin piece of code to deal with it.

orientationEventListener = object: OrientationEventListener(this) {
            override fun onOrientationChanged(orientation: Int) {
                val isPortrait = orientation > 300 || orientation < 60 || orientation in 120..240

                if ((requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT && isPortrait) ||
                    (requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE && !isPortrait)){
                    requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR


Try to setRequestedOrientation to SCREEN_ORIENTATION_SENSOR when exit from fullscreen !
setting OrientationEventListener may cause some unexpected resources usage.