Lock screen orientation (Android)

316,467

Solution 1

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.

Solution 2

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.

Solution 3

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"
Share:
316,467

Related videos on Youtube

user573536
Author by

user573536

Updated on February 03, 2020

Comments

  • user573536
    user573536 over 4 years

    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?

    • Kent
      Kent about 11 years
      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.
    • caw
      caw over 9 years
      This seems to be what you want: Call Screen.lockOrientation(this) and later Screen.unlockOrientation(this) from github.com/delight-im/Android-BaseLib/blob/master/Source/src‌​/im/…
    • Akshay
      Akshay almost 9 years
      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
    • Abandoned Cart
      Abandoned Cart about 6 years
      For future readers, @caw 's method is not an accessible method from within the context of a regular Android application.
    • caw
      caw about 6 years
      @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: github.com/delight-im/Android-Commons/blob/…
    • Abandoned Cart
      Abandoned Cart about 6 years
      @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.
    • caw
      caw about 6 years
      @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.
    • Abandoned Cart
      Abandoned Cart about 6 years
      @caw This isn't really the place to explain it. No need to hijack someone else's answer to get feedback on your own.
  • user573536
    user573536 over 13 years
    It doesn't seem to work; am I missing something?
  • Kevin Dion
    Kevin Dion over 13 years
    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
  • CoDe
    CoDe about 11 years
    is there any way to defined orientation for service..basically I write an LiveWallpaper service...where need to restrict device orientation.
  • nikoo28
    nikoo28 about 10 years
    this rotates my screen only clockwise, how can I rotate it anticlockwise?
  • Xplosive
    Xplosive over 9 years
    Thanks @KevinDion it works for me
  • Akshay
    Akshay almost 9 years
    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
  • bluehallu
    bluehallu over 8 years
    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‌​"