[ANDROID] Prevent Activity from Auto-Rotating

hi,

Today topic is about how to prevent the activity ( not the whole system ) from rotating.

it’s quite simple, using the following lines of code:

private void adjustRotating(boolean isOff) {
if (isOff) {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
} else {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
}

Want to turn of rotating, call adjustRotating(true) and to turn it on back to normal, call adjustRotating(false)

No other settings in Manifest or anywhere else.

This is some sample code, you can take it from Mediafire http://www.mediafire.com/?pnx9tqmje8pkf6q

——
References :

http://stackoverflow.com/questions/9941038/it-is-possible-to-lock-the-screen-orientation-of-the-phone-only-when-you-want
——
Just one small problem when you try this snippset of code is that when you disable rotating detection
in your activity, if you’re in landscape mode, this code would turn your phone to default mode ( portrait mode).
So be careful when using this !


Leave a comment