Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lock orientation during runtime

Is there a way to lock orientation during runtime? For example I'd like to allow the user to lock the screen to landscape if the user currently in landscape and toggle the menu option.

like image 212
Jared Avatar asked Mar 02 '10 20:03

Jared


People also ask

How do I lock screen orientation?

If you don't want the screen to switch between portrait and landscape when you move the device, you can lock the screen orientation. To do this, swipe down from the right side of the top panel. Hold the device in the orientation in which you want it locked. On the drop-down menu, touch the “Auto Rotate” button.

How do I lock orientation in android programmatically?

setRequestedOrientation(ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE); Called on an activity, will lock it to landscape.

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.


2 Answers

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

Called on an activity, will lock it to landscape. Look for the other flags in the ActivityInfo class. You can lock it back to portrait or make it sensor/slider driven.

More info here: http://www.devx.com/wireless/Article/40792

like image 157
haseman Avatar answered Nov 09 '22 02:11

haseman


Be careful of the difference between what getConfiguration returns and what setRequestedOrientation wants - they are both int, but they are coming from different constant definitions.

Here's how to lock the current orientation, while allowing 180 degree flips

int currentOrientation = getResources().getConfiguration().orientation; if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); } else {    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); } 
like image 37
Andy Weinstein Avatar answered Nov 09 '22 01:11

Andy Weinstein