Android: Temporarily Disable Orientation Changes in an Activity

Android: Temporarily disable orientation changes in an Activity

As explained by Chris in his self-answer, calling

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

and then

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

really works like charm... on real devices !

Don't think that it's broken when testing on the emulator, the ctrl+F11 shortcut ALWAYS change the screen orientation, without emulating sensors moves.

EDIT: this was not the best possible answer. As explained in the comments, there are issues with this method. The real answer is here.

How to enable/disable orientation change only for my app?

To temporally disable orientation changes in an Activity call this:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

and then to re-enable orientation changes call:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

Android: Prevent Orientation to change programmatically

I got it to work properly in all cases now.

To fix the screen:

if (getWindowManager().getDefaultDisplay().getRotation()== Surface.ROTATION_0)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (getWindowManager().getDefaultDisplay().getRotation()== Surface.ROTATION_90)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
if (getWindowManager().getDefaultDisplay().getRotation()== Surface.ROTATION_270)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);

and then to allow rotations again:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

Oim~

How do I disable orientation change on Android?

Update April 2013: Don't do this. It wasn't a good idea in 2009 when I first answered the question and it really isn't a good idea now. See this answer by hackbod for reasons:

Avoid reloading activity with asynctask on orientation change in android

Add android:configChanges="keyboardHidden|orientation" to your AndroidManifest.xml. This tells the system what configuration changes you are going to handle yourself - in this case by doing nothing.

<activity android:name="MainActivity"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">

See Developer reference configChanges for more details.

However, your application can be interrupted at any time, e.g. by a phone call, so you really should add code to save the state of your application when it is paused.

Update: As of Android 3.2, you also need to add "screenSize":

<activity
android:name="MainActivity"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize">

From Developer guide Handling the Configuration Change Yourself

Caution: Beginning with Android 3.2 (API level 13), the "screen size"
also changes when the device switches between portrait and landscape
orientation. Thus, if you want to prevent runtime restarts due to
orientation change when developing for API level 13 or higher (as
declared by the minSdkVersion and targetSdkVersion attributes), you
must include the "screenSize" value in addition to the "orientation"
value. That is, you must declare
android:configChanges="orientation|screenSize". However, if your
application targets API level 12 or lower, then your activity always
handles this configuration change itself (this configuration change
does not restart your activity, even when running on an Android 3.2 or
higher device).

How to disable orientation changes for main activity?

Add this to the MainActivity in AndroidManifest.xml:

<activity
...
android:screenOrientation="landscape"
... >
</activity>

How to prevent Screen Orientation change when Activity finishes on Android 8.1 Version Devices?

This bug was fixed. So waiting for next release/patch.

Programmatically disabling screen rotations in the entire Android application

As in keep it in portrait or Landscape no matter which way device is tilted?
You need to add this to your manifest inside the activity that you want to limit

 android:screenOrientation="portrait" //Or landscape for horizontal.

If you want it different between activities just make your manifest look like this.

        <activity android:name=".Activity1" 
android:screenOrientation="landscape">
</activity>

<activity android:name=".Activity2"
android:screenOrientation="portrait">
</activity>

<activity android:name=".Activity3"
android:screenOrientation="landscape">
</activity>

Disable the orientation change

try this in your code...

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

get the orientation in oncreate and set that orientation for that activity



Related Topics



Leave a reply



Submit