Lock Screen Orientation in Android

 
Martin Belcher 26th May 2010

IntroAndroid screen rotation image

This article describes how to force the orientation of an Android view not to change ie screen not to rotate.


How to lock the orientation

In the onCreateDialog(int) event of the activity use the setRequestedOrientation(int) method to set the screen orientation to your chosen setting. The activity will stay in this orientation regardless of if the device is tilted or not.

[Code sample – How to lock the orientation]
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState
{
    super.onCreate(savedInstanceState);
    this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

How to detect the current orientation

To programmatically detect the current orientation of the activity use the following code snippet. The orientation property of the Configuration class returns three possible values corresponding to Landscape, Portrait and Square.

[Code sample – How to detect the current orientation]
switch (this.getResources().getConfiguration().orientation)
{
case Configuration.ORIENTATION_PORTRAIT:
  // Do something here
  break;
case Configuration.ORIENTATION_LANDSCAPE:
  // Do something here
  break;
case Configuration.ORIENTATION_SQUARE:
  // Do something here
  break;
default:
  throw new Exception("Unexpected orientation enumeration returned");
  break;
}

Example : Locking rotation while performing an action.

You might wish to disable the screen rotation whilst performing an action or by user command, to do this you need to combine the above samples to detect the current orientation and lock the display to that orientation.

[Code sample – Locking rotation while performing an action]
// Sets screen rotation as fixed to current rotation setting
private void mLockScreenRotation()
{
  // Stop the screen orientation changing during an event
    switch (this.getResources().getConfiguration().orientation)
    {
  case Configuration.ORIENTATION_PORTRAIT:
    this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    break;
  case Configuration.ORIENTATION_LANDSCAPE:
    this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    break;
    }
}

Once your action has completed you may wish to enable screen rotation again, see the next section for an example on how to do this.


How to re-enable screen rotation

To enable the orientation to be automatically changed on device tilt simply pass the setRequestedOrientation(int) method the enumeration value for an unspecified orientation.

[Code sample – How to re-enable screen rotation]
// allow screen rotations again
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
 

Comments

 
Rajesh 1st December 2010
hi it's working fine, Thanks for giving
david 27th January 2011
I tried in the regular activity as well. mLockScreenOrientation is NOT working. It still lets it change orientation.
Martin Belcher 10th February 2011
@david Have you tried writing the value of
this.getResources().getConfiguration().orientation
to LogCat?

If this is some other value than ORIENTATION_PORTRAIT or ORIENTATION_LANDSCAPE then you may want to add a further case to the switch statement.
wbu 8th March 2011
Do you know how to make it work with SCREEN_ORIENTATION_REVERSE_LANDSCAPE and SCREEN_ORIENTATION_REVERSE_PORTRAIT too? What i am missing is a mapping of the Surface.ORIENTATION_XXX to the ActivityInfo.SCREEN_ORIENTATION_XXX depending on the devices "natural" orientation.
yongrak0 20th April 2011
@wbu, Unfortunately, that REVERSE things are supported after 2.3 android version. There's no way to lock reversed screen.
anthony 3rd June 2011
For all directions you can use int rotation = this.getWindowManager().getDefaultDisplay().getRotation(); this.setRequestedOrientation(rotation);
Zachar 6th August 2011
Hello. Is there any way to disable activity restarts when orientation change occurs? The way with setRequestedOrientation does not help with this. I don't understand why system still restarts applications with locked orientation.
Dave 23rd September 2011
I need to force my app to portrait, it just won't work in landscape. If I call setRequestedOrientation() for portrait but it's already in landscape, the app crashes. stackoverflow is down, any thoughts? thanks, dave
Martin Belcher 23rd September 2011
@Dave I am not sure why your app crashes you will need to inspect the debug output but you could force your activity to start in portrait by setting the android:screenOrientation attribute in the activity declaration of your manifest file.

<activity android:screenOrientation="portrait" />
roshi 4th November 2011
thanks so much helped a lot
Ian Mcmillan 19th April 2012
I am using gingerbread on a galaxy tab. Using your advice i am able to program device to control orientation ok. However my machine, other in these specific s is locked into portrait! Orientation lock widget is unresponsive. How can i get machine to control orientation screen in general operation?
 

Add Comment

 
Name:
Email:
This email address will not be shown on the website and will not be distributed to any other parties. By submitting this email address you agree to receive an email from Eigo in the future, that email will include an option to unsubscribe from future emails.
Comment:
(HTML allowed)
Website: (Optional)
A link to show beneath your comment.
Security Check: