Enable and Disable Auto Rotate Programmatically

Enable and disable auto rotate programmatically?

This should do the trick for you:

    import android.provider.Settings;
public static void setAutoOrientationEnabled(Context context, boolean enabled)
{
Settings.System.putInt( context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}

Add permission to the AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

You can find the documentation here

Programmatically enabling/disabling screen rotations in Android

I would do this by having my preference page write to the SharedPreferences for my application, then reading this value and calling Activity.setRequestedOrientation() from the code when the text view in question is loaded or displayed.

For example:

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (prefs.getBoolean("fix_orientation_to_portrait", false)) {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}

This can be in either the onCreate() or onResume() method. The only reason not to have it in the onCreate() method is if the changing of the setting happens in this activity, or on a child activity that will come back to this one when it is done.

Edit:

OK, if that doesn't work (and I am not really sure it will), maybe you could try having two different layout xml files - one with the screenOrientation set, and the other without. When you load your Activity/View, you can dynamically load one or the other based on your saved preferences. It's nasty not clean, but it should work.

Prevent screen rotation on Android

Add

android:screenOrientation="portrait" 

or

 android:screenOrientation="landscape" 

to the <activity> element/s in
the manifest and you're done.

Enable and disable auto rotate programmatically using Swift?

You could create an action for the button that sets a boolean flag somewhere in your code and return the value of that flag in the shouldAutorotate method of the view controller. If you need that for all view controllers you could create a common base view controller (inheritance).

Example of button action:

@IBAction func toggleRotation(sender: Button) {
// A made up AppConfig class with class method for setting and retrieving
// rotation flag.
AppConfig.allowRotation(!AppConfig.allowRotation)
}

Example of shouldAutorotate:

override func shouldAutorotate() -> Bool {
return AppConfig.allowRotation()
}

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instm/UIViewController/shouldAutorotate

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>

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);

iOS. How to enable and disable rotation on each UIViewController?

I have the answer.
On AppDelegate, if you rotate device, push viewcontroller, etc. This function always call

Update for swift 3/4

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
{
return self.restrictRotation
}

self.restrictRotation is a custom parameter.

How to use:

In Appdelegate:

 var restrictRotation:UIInterfaceOrientationMask = .portrait

In ViewController:

When method ViewDidLoad or viewWillAppear is called. We will change like this:

(UIApplication.shared.delegate as! AppDelegate).restrictRotation = .all 

and then this method on AppDelegate will be called.

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask


Related Topics



Leave a reply



Submit