Lock screen orientation (Android)
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"
Related videos on Youtube
user573536
Updated on February 03, 2020Comments
-
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 about 11 yearsThe "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 over 9 yearsThis seems to be what you want: Call
Screen.lockOrientation(this)
and laterScreen.unlockOrientation(this)
from github.com/delight-im/Android-BaseLib/blob/master/Source/src/im/… -
Akshay almost 9 yearsJust 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 about 6 yearsFor future readers, @caw 's method is not an accessible method from within the context of a regular Android application.
-
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
andScreen.unlockOrientation
: github.com/delight-im/Android-Commons/blob/… -
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 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 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 over 13 yearsIt doesn't seem to work; am I missing something?
-
Kevin Dion over 13 yearsHmm 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 inonResume
, notonCreate
, and in the non-landscape Activity useActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
instead ofSENSOR
. My emulator bugged out too with sensor, but unspecified worked -
CoDe about 11 yearsis there any way to defined orientation for service..basically I write an LiveWallpaper service...where need to restrict device orientation.
-
nikoo28 about 10 yearsthis rotates my screen only clockwise, how can I rotate it anticlockwise?
-
Xplosive over 9 yearsThanks @KevinDion it works for me
-
Akshay almost 9 yearsJust 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 over 8 yearsWarning: 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"