161

I'm writing an android application that uses tabs with different contents (activities). In one of these activities, I would like to lock the screen orientation to "Landscape"-mode, but in the other activities, I want the normal orientation (according to sensor).

What I'm doing now is that I'm calling

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

when I switch to the landscape mode activity, and

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

when I switch back to the other activities. However, this doesn't seem to work, the whole application locks up. What is the normal approach to this problem?

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
user573536
  • 2,335
  • 3
  • 17
  • 8
  • 23
    The "Possible Duplicate" link is **not** a duplicate question to this one. This question is asking how to **lock the orientation** so that it cannot change from landscape. The linked question is asking how to **prevent application restarts** when the orientation does change. – Kent Apr 22 '13 at 22:35
  • This seems to be what you want: Call `Screen.lockOrientation(this)` and later `Screen.unlockOrientation(this)` from https://github.com/delight-im/Android-BaseLib/blob/master/Source/src/im/delight/android/baselib/Screen.java – caw Mar 04 '15 at 20:37
  • Just a caution though, if you are using inline ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT, this is not allowed below 4.3 . You can then use above or ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT – Akshay Jul 23 '15 at 21:57
  • For future readers, @caw 's method is not an accessible method from within the context of a regular Android application. – Abandoned Cart May 01 '18 at 15:28
  • @LoungeKatt No, it’s not, but I did link to the implementation, didn’t I? Anyway, I just saw that the link is not valid anymore. Here’s an updated link to the source for both `Screen.lockOrientation` and `Screen.unlockOrientation`: https://github.com/delight-im/Android-Commons/blob/f220021846f66917d56d465c7150bce9b585c08d/Source/library/src/main/java/im/delight/android/commons/Screen.java#L48 – caw May 01 '18 at 20:40
  • @caw As you have already noticed, the link was broken when making the comment. It seems like a rather extravagant solution, so it seemed good to point out that it wasn't part of the API. – Abandoned Cart May 02 '18 at 06:20
  • @LoungeKatt Sure. But what exactly is extravagant about it? It’s quite a pragmatic approach: It detects the current orientation (which has been determined automatically by the OS) and then sets that orientation explicitly in order to lock it. – caw May 02 '18 at 18:57
  • @caw This isn't really the place to explain it. No need to hijack someone else's answer to get feedback on your own. – Abandoned Cart May 03 '18 at 03:01

3 Answers3

258

In the Manifest, you can set the screenOrientation to landscape. It would look something like this in the XML:

<activity android:name="MyActivity"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation|screenSize">
...
</activity>

Where MyActivity is the one you want to stay in landscape.

The android:configChanges=... line prevents onResume(), onPause() from being called when the screen is rotated. Without this line, the rotation will stay as you requested but the calls will still be made.

Note: keyboardHidden and orientation are required for < Android 3.2 (API level 13), and all three options are required 3.2 or above, not just orientation.

Tigger
  • 8,980
  • 5
  • 36
  • 40
Kevin Dion
  • 4,027
  • 1
  • 17
  • 13
  • It doesn't seem to work; am I missing something? – user573536 Jan 13 '11 at 01:10
  • 3
    Hmm yeah you're right that doesn't seem to work if the Activity is running inside a TabHost. I think you might have the right idea using `setRequestedOrientation`, but try putting the call in `onResume`, *not* `onCreate`, and in the non-landscape Activity use `ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED` instead of `SENSOR`. My emulator bugged out too with sensor, but unspecified worked – Kevin Dion Jan 13 '11 at 02:12
  • is there any way to defined orientation for service..basically I write an LiveWallpaper service...where need to restrict device orientation. – CoDe Jun 18 '13 at 12:31
  • this rotates my screen only clockwise, how can I rotate it anticlockwise? – nikoo28 Apr 11 '14 at 11:49
  • This method has so many disadvantages to it and is considered an anti-pattern since 2016. Better to learn the Activity lifecycle, particularly _what_ events call when you rotate the device. I'm currently looking for a solution to this however. – Tim Kist Jan 24 '23 at 14:47
65

I had a similar problem.

When I entered

<activity android:name="MyActivity" android:screenOrientation="landscape"></activity>

In the manifest file this caused that activity to display in landscape. However when I returned to previous activities they displayed in lanscape even though they were set to portrait. However by adding

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

immediately after the OnCreate section of the target activity resolved the problem. So I now use both methods.

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
Alan Cartwright
  • 651
  • 5
  • 2
  • 1
    Just a caution though, if you are using ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT, this is not allowed below 4.3 . You can then use above or ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT – Akshay Jul 23 '15 at 21:56
  • 1
    Warning: Using setRequestedOrientation() will cause the Activity to restart, triggering onResume etc twice. Add this to your manifest for every activity where you do this to avoid it: android:configChanges="keyboardHidden|orientation|screenSize" – bluehallu Mar 24 '16 at 14:17
40

inside the Android manifest file of your project, find the activity declaration of whose you want to fix the orientation and add the following piece of code ,

android:screenOrientation="landscape"

for landscape orientation and for portrait add the following code,

android:screenOrientation="portrait"
Linh
  • 57,942
  • 23
  • 262
  • 279
HjK
  • 3,513
  • 1
  • 17
  • 23