How to Lock Orientation During Runtime

How to lock orientation during runtime

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Called on an activity, will lock it to landscape. Look for the other flags in the ActivityInfo class. You can lock it back to portrait or make it sensor/slider driven.

More info here: http://www.devx.com/wireless/Article/40792

How to lock the screen orientation of the fragment to landscape at runtime

I have implemented the same functionality myself. My app is locked to Portrait mode for all fragments except one. In order to achieve the correct functionality I use android:screenOrientation="nosensor" for the activity (in the Manifest file) preventing orientation changes due to the user rotating the device.

I also use android:configChanges="keyboardHidden|orientation|screenSize" to prevent the activity to restart when the orientation is changed. Otherwise, it's easy to end up in a loop where the activity restarts, goes into the correct fragment and changes the orientation again, repeat.

In the Fragment's onResume() method I call getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); and in it's onPause() I call getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Hope this helps you!

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>

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 lock orientation of one view controller to portrait mode only in Swift

Things can get quite messy when you have a complicated view hierarchy, like having multiple navigation controllers and/or tab view controllers.

This implementation puts it on the individual view controllers to set when they would like to lock orientations, instead of relying on the App Delegate to find them by iterating through subviews.

Swift 3, 4, 5

In AppDelegate:

/// set orientations you want to be allowed in this property by default
var orientationLock = UIInterfaceOrientationMask.all

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

In some other global struct or helper class, here I created AppUtility:

struct AppUtility {

static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {

if let delegate = UIApplication.shared.delegate as? AppDelegate {
delegate.orientationLock = orientation
}
}

/// OPTIONAL Added method to adjust lock and rotate to the desired orientation
static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {

self.lockOrientation(orientation)

UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
}

}

Then in the desired ViewController you want to lock orientations:

 override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

AppUtility.lockOrientation(.portrait)
// Or to rotate and lock
// AppUtility.lockOrientation(.portrait, andRotateTo: .portrait)

}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

// Don't forget to reset when view is being removed
AppUtility.lockOrientation(.all)
}

If iPad or Universal App

Make sure that "Requires full screen" is checked in Target Settings -> General -> Deployment Info. supportedInterfaceOrientationsFor delegate will not get called if that is not checked.
Sample Image

Flutter: How to set and lock screen orientation on-demand

First import the services package:

import 'package:flutter/services.dart';

This will give you access to the SystemChrome class, which "Controls specific aspects of the operating system's graphical interface and how it interacts with the application."

When you load the Widget, do something like this:

@override
void initState(){
super.initState();
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
]);
}

then when I leave the page, put it back to normal like this:

@override
dispose(){
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
super.dispose();
}


Related Topics



Leave a reply



Submit