Is there a way to lock orientation during runtime? For example I'd like to allow the user to lock the screen to landscape if the user currently in landscape and toggle the menu option.
7 Answers
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Called on an activity, will lock it to landscape. Look for the other flags in the ActivityInfo class. You can lock it back to portrait or make it sensor/slider driven.
More info here: http://www.devx.com/wireless/Article/40792
-
14Ok thanks. This works great. That will get the current orientation. getResources().getConfiguration().orientation – Jared Mar 02 '10 at 21:16
-
7Careful! You need to differentate between what getConfiguration() returns and what setRequestedOrientation wants - see my answer below for details – Andy Weinstein May 07 '12 at 19:31
-
there is an issue with this approach. Make sure you go through the comment of [this answer](http://stackoverflow.com/a/3614089/1708390) – Bugs Happen Mar 28 '15 at 12:15
-
but it sets the same orientation for all the activity, is there anyother way where we can change the orientation – silverFoxA May 02 '15 at 13:18
Be careful of the difference between what getConfiguration returns and what setRequestedOrientation wants - they are both int, but they are coming from different constant definitions.
Here's how to lock the current orientation, while allowing 180 degree flips
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}

- 2,639
- 3
- 21
- 32
-
13You might prefer to use SCREEN_ORIENTATION_USER_LANDSCAPE as this does not allow the 180 degree flips if the user has disabled screen rotation in the settings. Similarly when switching back to free rotation, SCREEN_ORIENTATION_USER is better than SCREEN_ORIENTATION_SENSOR as the latter allows free rotation even if the settings say not to. – Steve Waring Aug 01 '14 at 12:52
-
Brilliant! Need to add that when you switch to reverse orientation reconfiguration doesn't occur. At least on devices I've tested it on. It's really important to know if you want to stop reconfiguration during some dialog shows etc – sberezin Jul 03 '15 at 10:08
-
This works on devices with reverse portrait and reverse landscape.
Lock orientation:
int orientation = getActivity().getRequestedOrientation(); int rotation = ((WindowManager) getActivity().getSystemService( Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); switch (rotation) { case Surface.ROTATION_0: orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; break; case Surface.ROTATION_90: orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; break; case Surface.ROTATION_180: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; break; default: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; break; } getActivity().setRequestedOrientation(orientation);
Unlock orientation:
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

- 2,531
- 1
- 20
- 20
-
5Get rotation `"Returns the rotation of the screen from its "natural" orientation."` [source](http://developer.android.com/reference/android/view/Display.html#getRotation()). So on a phone saying ROTATION_0 is portrait is likely correct, but on a tablet its "natural" orientation is likely landscape and ROTATION_0 should return landscape instead of portrait. – jp36 Jan 28 '13 at 15:11
-
@jp36, I tested on a Nexus 7 which has a natural orientation that is the same as a phone. Thanks for testing on a bigger tablet (which I don't have). – pstoppani Jan 28 '13 at 18:33
-
1As jp36 said, it doesn't work on tablets with a natural landscape orientation! – DominicM Feb 22 '15 at 13:45
-
I seemed to had have a similar case. I wanted to support any orientation, but I needed to stay in the current orientation after a certain point in the workflow. My solution was:
At entry of the protected workflow:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
At exit of the protected workflow:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

- 9,413
- 4
- 33
- 40
-
2This doesn't address the OQ, at least on Android >= 16. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR) might set the device to landscape even if it was in portrait, whereas the question refers to locking orientation that was given. – greg7gkb Jul 26 '13 at 19:42
-
5to me, setting it to nosensor takes me back to portrait mode if I was on landscape I used SCREEN_ORIENTATION_LOCKED instead and it worked for me – Jimmar Aug 31 '14 at 14:48
-
2@JiMMaR SCREEN_ORIENTATION_LOCKED is the best way for Android >= 18. But in case you target something lower, that does not work. I suggest using jp36's answer below. – Patrick Boos Jan 05 '15 at 06:49
Alternative to @pstoppani answer with support for tablets (As with @pstoppani answer, this will only work on devices >2.2)
-Tested on Samsung Galaxy SIII
and Samsung Galaxy Tab 10.1
public static void lockOrientation(Activity activity) {
Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();
int tempOrientation = activity.getResources().getConfiguration().orientation;
int orientation = 0;
switch(tempOrientation)
{
case Configuration.ORIENTATION_LANDSCAPE:
if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
else
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
break;
case Configuration.ORIENTATION_PORTRAIT:
if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270)
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
else
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
activity.setRequestedOrientation(orientation);
}

- 1,873
- 18
- 28
-
Thanks, It's working fine but I didn't understand that why it's check with `||` in `rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90` and with `rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270`. So I have 2 doubt:::: first, Why `ROTATION_0` instead of `ROTATION_180` in second case and another why check 0 degree with 90 not 180 ?? – AndyBoy Aug 12 '19 at 08:20
-
@AndyBoy it has to do with the default orientation of devices. Typically phones have a default orientation of portrait which means the rotation returns zero for portrait, but some tablets have a default of landscape which means the rotation returns zero for landscape. So the different `||` checks are handling the two possible default orientations based on the device reporting landscape vs portrait. – jp36 Aug 13 '19 at 17:54
Here is my code, you could lock with one of these methods your screen and once finished the task unlock it with unlockOrientation:
/** Static methods related to device orientation. */
public class OrientationUtils {
private OrientationUtils() {}
/** Locks the device window in landscape mode. */
public static void lockOrientationLandscape(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
/** Locks the device window in portrait mode. */
public static void lockOrientationPortrait(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
/** Locks the device window in actual screen mode. */
public static void lockOrientation(Activity activity) {
final int orientation = activity.getResources().getConfiguration().orientation;
final int rotation = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
// 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;
// Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO
if (!BuildVersionUtils.hasGingerbread()) {
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);
}
}
}
/** Unlocks the device window in user defined screen mode. */
public static void unlockOrientation(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
}
}

- 6,723
- 4
- 55
- 59
Here is the Xamarin conversion of @pstoppani 's answer above.
NOTE: this is for a Fragment, replace Activity. with this. if used within an activity.
public void LockRotation()
{
ScreenOrientation orientation;
var surfaceOrientation = Activity.WindowManager.DefaultDisplay.Rotation;
switch (surfaceOrientation) {
case SurfaceOrientation.Rotation0:
orientation = ScreenOrientation.Portrait;
break;
case SurfaceOrientation.Rotation90:
orientation = ScreenOrientation.Landscape;
break;
case SurfaceOrientation.Rotation180:
orientation = ScreenOrientation.ReversePortrait;
break;
default:
orientation = ScreenOrientation.ReverseLandscape;
break;
}
Activity.RequestedOrientation = orientation;
}
public void UnlockRotation()
{
Activity.RequestedOrientation = ScreenOrientation.Unspecified;
}
This is untested as went with a different approach before used it, but may be of use.

- 2,331
- 4
- 24
- 54
-
This is the same answer as pstoppani's, and it will fail on a tablet. – Tim Autin Jan 03 '17 at 17:28