16

In my manifest I've setup an activity restricted to portrait orientation. But I need to remove this restriction on condition. So, how do I achieve removing orientation restrictions programmatically ?

upd: my present settings are:

    <activity
        android:name=".activity.MainActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar"
        android:screenOrientation="portrait"
        android:configChanges="orientation">


 /**
 * Defines whether the device being used is a tablet and if so adds horizontal orientation option.
 */
     protected void _updateScreenOrientationModes(){
         if(((MyApplication) getApplication())._isTablet == true)
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
         }
user1384991
  • 2,559
  • 3
  • 17
  • 20

3 Answers3

20

Whether or not you've set android:screenOrientation in your Manifest, you can programmatically set the orientation with Activity.setRequestedOrientation().

In essence, "removing the restriction" is accomplished by

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

after which the Activity will exhibit the default behavior of changing screen orientation when the physical device orientation changes.

It's likely, however, that you want to actually match the current physical device orientation at the same time. I haven't tried it, but I'm pretty sure that if all you did was the above, if the device was physically in a landscape orientation when you did it, then you'd stay in portrait mode until you physically moved the device to portrait and back to landscape.

What I would do is be sure to set android:configChanges="orientation" in your Manifest and override Activity.onConfigurationChanged(), in which you can, according to your condition, either perform the orientation change or cache the orientation. Then whenever your condition changes, you'll have the current physical orientation handy so you can change it at that point if necessary.

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
2

Programmatically you can change your screen orientations by using "setRequestedOrientation()"

In your java class write the following code as per your condition required.....

To change to portrait mode, use the ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE constant:

     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

To change to portrait mode, use the ActivityInfo.SCREEN_ORIENTATION_PORTRAIT constant:

   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
code_finder
  • 1,370
  • 2
  • 21
  • 39
  • I meant to remove restriction to allow Android decide which orientation should be used, so if I have a portrait defined in Manifest, but I need to remove this restriction - it's not clear to which parameter orientation should be set - user might be holding his device in landscape as well as portrait orientation - how do I define which is used ?.I need to allow tablets to have both orientations and handsets only portrait.Regarding your answer - when a tablet user launches app how do I define his **holding** orientation and set layout to appropriate ? – user1384991 May 31 '12 at 08:40
  • Additionally, I'm afraid this method doesn't allow for both orientations since it restricts for one particular. I've tried so far SCREEN_ORIENTATION_UNSPECIFIED but in vain – user1384991 May 31 '12 at 08:41
  • 1
    The following SCREEN-ORIENTATION-UNSPECIFIED works, i used it. what ever you represent in your manifest, if you specify this UNSPECIFIED call in your java code means by default it will display in Portrait mode. – code_finder May 31 '12 at 09:37
2

I'm going to leave my other answer, as it addresses the more general question that you asked. However, in a comment to someone else you said:

I need to allow tablets to have both orientations and handsets only portrait

Your particular case is actually easier than the general case: Remove both android:screenOrientation="portrait" and android:configChanges="orientation" from your Manifest to allow the default behavior. Then, during startup, if the device is a handset, force portrait orientation with

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

A device will obviously never change between being a tablet or a handset at runtime, so you only need to do this once, at startup. Tablets will get the default behavior, so whatever physical orientation the device is in, that's what they'll use. Handsets will be forced into portrait and stay that way.

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109