[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 !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: