Remove orientation restricitons programmatically

10,129

Solution 1

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.

Solution 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.

Solution 3

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);
Share:
10,129

Related videos on Youtube

user1384991
Author by

user1384991

Android is so trashed !

Updated on September 16, 2022

Comments

  • user1384991
    user1384991 about 1 year

    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
    user1384991 over 11 years
    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
    user1384991 over 11 years
    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
    user1384991 over 11 years
    I've thought about it as well :). But method with UNSPECIFIED doesn't work.
  • Darshan Rivka Whittle
    Darshan Rivka Whittle over 11 years
    It definitely works for me on quite a few devices. Are you sure you're actually reaching the call to setRequestedOrientation()?
  • code_finder
    code_finder over 11 years
    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.
  • Darshan Rivka Whittle
    Darshan Rivka Whittle over 11 years
    Actually, I think you can keep things much simpler, now that I know your condition is just "is it a tablet?" -- see my other answer.
  • dy_
    dy_ about 8 years
    after setting this flag, i am stuck in portrait mode :( nexus 10