I am using the following code to set orientation locking per user preference:
private void doLock(boolean locked) {
if (locked) {
int o = getResources().getConfiguration().orientation;
if (o == Configuration.ORIENTATION_LANDSCAPE)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
else if (o == Configuration.ORIENTATION_PORTRAIT)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
}
It works, except for the case in which I am in unlocked mode (SCREEN_ORIENTATION_SENSOR
) with the screen showing LANDSCAPE
correctly (!), then invoke doLock(true)
and...
instead of the screen locking to LANDSCAPE
in its current (correct) landscape view, it locks to an upside down landscape view. i.e. same exact but flip vertically (y
becomes -y
).
Why is that and how do I approach this problem in order to fix it?
My initial inquiry reveals that there are quite a few possibilities other than the common two (portrait
, landscape
), including reverseLandscape
, but my hunch tells me that this problem may be device-dependent and so by using it I might be fixing the problem only for my phone but not for all other devices.
Is there a way to force correct landscape
orientation (when switched from sensor
) in all devices?
To make this clearer and easier to reproduce, here are the steps that exhibit the problem:
SCREEN_ORIENTATION_SENSOR
) with the screen showing LANDSCAPE
correctly (!), doLock(true)
LANDSCAPE
in its current (correct) landscape view, it locks to an upside down landscape view. i.e. same exact but flip vertically (y
becomes -y
).Head into Settings –> Display and uncheck Auto-rotate screen. That's all there is to it.
If the screen rotation is already on try turning it off and then on again. To check this setting, you can swipe down from the top of the display. If it's not there, try going to Settings > Display > Screen rotation.
Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off.
Try the following to disable and enable orientation via code (this will work in API level 7 and up) :
public static void disableRotation(Activity activity) {
final int orientation = activity.getResources().getConfiguration().orientation;
final int rotation = activity.getWindowManager().getDefaultDisplay()
.getOrientation();
// Copied from Android docs, since we don't have these values in Froyo
// 2.2
int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8;
int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO) {
SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
} else if (rotation == Surface.ROTATION_180
|| rotation == Surface.ROTATION_270) {
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}
}
}
public static void enableRotation(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With