Skip to content Skip to sidebar Skip to footer

Android - Rotate Screen Programmatically Without Locking It

I'm trying to achieve this behaviour: When I rotate the device to landscape, the screen does it too. To get it back to portrait there are two ways: Rotating the device Clicking

Solution 1:

I'v found this approach very nice:

// E.g. fullscreen button pressed - set landscape orientation;// it will disable orientation by the sensor.setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE)

// Assume now user rotated the device... or not.// Anyway enable orientation by the sensor after few seconds, so user may use app normal way.
someView.postDelayed(
    { setRequestedOrientation(SCREEN_ORIENTATION_SENSOR) }, delay = 5000L)

Solution 2:

I had the exact 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.

OrientationEventListenerorientationEventListener=newOrientationEventListener(getActivity()) {
@OverridepublicvoidonOrientationChanged(int orientation) {
    intepsilon=10;
    intleftLandscape=90;
    intrightLandscape=270;
    if(epsilonCheck(orientation, leftLandscape, epsilon) ||
       epsilonCheck(orientation, rightLandscape, epsilon)){
            getMainActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
        }
    }

    privatebooleanepsilonCheck(int a, int b, int epsilon) {
        return a > b - epsilon && a < b + epsilon;
    }
};
orientationEventListener.enable();

Solution 3:

Firstly put your Auto rotation On from Android setting.

then use the below code on your Activity where you are configuring Video to Landscape mode.

override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig)

    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        // if you tilt your screen to Portrait mode// send your seek position with intent to continue watching video to your previous screenfinish()
    }else{
       // continue to playing video
    }
}

In your manifest file put screenOrientation to "fullSensor" like below:

android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode"
        android:screenOrientation="fullSensor"

Solution 4:

In your manifest file put screenOrientation to "fullSensor" like below:

android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode" android:screenOrientation="fullSensor"

Solution 5:

Read this USER ROTATION

Use this:

android.provider.Settings.System.putInt(getContentResolver(), android.provider.Settings.System.USER ROTATION,user_rotation);

user_rotation could be 0, 1, 2 or 3;

  • 0 == 0º
  • 1 == 90º
  • 2 == 180º
  • 3 == 270º

You need to add the next line in the manifest.

<uses-permissionandroid:name="android.permission.WRITE_SETTINGS"></uses-permission>

PD: If you had tried to search, you could have found something. Like in this Post by Riddhish.Chaudhari

Post a Comment for "Android - Rotate Screen Programmatically Without Locking It"