Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove orientation restricitons programmatically

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);
         }
like image 559
user1384991 Avatar asked May 31 '12 07:05

user1384991


2 Answers

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.

like image 125
Darshan Rivka Whittle Avatar answered Oct 05 '22 23:10

Darshan Rivka Whittle


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);
like image 44
code_finder Avatar answered Oct 05 '22 22:10

code_finder