Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lock screen orientation (Android) [duplicate]

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?

like image 309
user573536 Avatar asked Oct 10 '22 10:10

user573536


People also ask

How do I Lock screen orientation on android?

Touch and hold the Auto rotate icon to open its settings, and then review the options for the Home screen, Lock screen, and Voice call screen. You can turn on your preferred settings by tapping the switches next to them, and then tapping Done.

How do I force orientation on android?

Use Rotation Control to force screen orientation of Android devices. Once you've installed, open the app and just tap to turn it on. You can even start it when you restart your phone. This will help you when you don't want to open the again and again after every reboot.


1 Answers

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.

like image 257
Kevin Dion Avatar answered Oct 18 '22 18:10

Kevin Dion