How to set entire application in portrait mode only?
For any Android version
From XMLYou can specify android:screenOrientation="portrait"
for each activity in your manifest.xml file. You cannot specify this option on the application
tag.
Other option is to do it programmatically, for example in an Activity
base class:
@Override
public void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
For Android 4+ (API 14+)
Last option is to do it with activity lifecycle listeners which is only available since Android 4.0 (API 14+). Everything happens in a custom Application
class:
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityLifecycleAdapter() {
@Override
public void onActivityCreated(Activity a, Bundle savedInstanceState) {
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
});
}
ActivityLifecycleAdapter
is just a helper class you'll need to create which will be an empty implementation of ActivityLifecycleCallbacks
(so you don't have to override each and every methods of that interface when you simply need one of them).
I want my android application to be only run in portrait mode?
In the manifest, set this for all your activities:
<activity android:name=".YourActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"/>
Let me explain:
- With
android:configChanges="orientation"
you tell Android that you will be responsible of the changes of orientation. android:screenOrientation="portrait"
you set the default orientation mode.
How to set app to be portrait only?
As seen in the question:
I want my android application to be only run in portrait mode?
You should set the configChanges
and screenOrientation
flags for every activity, and not at the root of your manifest.
So it should look like this:
<activity android:name=".MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
How can I disable landscape mode in Android?
Add android:screenOrientation="portrait"
to the activity in the AndroidManifest.xml. For example:
<activity android:name=".SomeActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" />
Since this has become a super-popular answer, I feel very guilty as forcing portrait is rarely the right solution to the problems it's frequently applied to.
The major caveats with forced portrait:
- This does not absolve you of having to think about activity
lifecycle events or properly saving/restoring state. There are plenty of
things besides app rotation that can trigger an activity
destruction/recreation, including unavoidable things like multitasking. There are no shortcuts; learn to use bundles andretainInstance
fragments. - Keep in mind that unlike the fairly uniform iPhone experience, there are some devices where portrait is not the clearly popular orientation. When users are on devices with hardware keyboards or game pads a la the Nvidia Shield, on Chromebooks, on foldables, or on Samsung DeX, forcing portrait can make your app experience either limiting or a giant usability hassle. If your app doesn't have a strong UX argument that would lead to a negative experience for supporting other orientations, you should probably not force landscape. I'm talking about things like "this is a cash register app for one specific model of tablet always used in a fixed hardware dock."
So most apps should just let the phone sensors, software, and physical configuration make their own decision about how the user wants to interact with your app. A few cases you may still want to think about, though, if you're not happy with the default behavior of sensor
orientation in your use case:
- If your main concern is accidental orientation changes mid-activity that you think the device's sensors and software won't cope with well (for example, in a tilt-based game) consider supporting landscape and portrait, but using
nosensor
for the orientation. This forces landscape on most tablets and portrait on most phones, but I still wouldn't recommend this for most "normal" apps (some users just like to type in the landscape softkeyboard on their phones, and many tablet users read in portrait - and you should let them). - If you still need to force portrait for some reason,
sensorPortrait
may be better thanportrait
for Android 2.3 (Gingerbread) and later; this allows for upside-down portrait, which is quite common in tablet usage.
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.
Force portrait orientation mode
Don't apply the orientation to the application element, instead you should apply the attribute to the activity element, and you must also set configChanges
as noted below.
Example:
<activity
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
</activity>
This is applied in the manifest file AndroidManifest.xml
.
Related Topics
End Call in Android Programmatically
Notification to Restore a Task Rather Than a Specific Activity
How to Copy Text Programmatically in My Android App
Android - How to Unregister a Receiver Created in the Manifest
The APK File Does Not Exist on Disk
How to Handle Cookies in Httpurlconnection Using Cookiemanager
Why Doesn't Logcat Show Anything in My Android
Android: Using Findviewbyid() with a String/In a Loop
How to Do Http Authentication in Android
Get and Parse CSV File in Android
Audiorecord Object Not Initializing
How to Strip or Escape HTML Tags in Android
How to Set Dialog to Show in Full Screen
How to Set Id of Dynamic Created Layout
Android: Long Click on a Button -> Perform Actions
Is There a Realm Browser for Linux
"Parse Error: There Is a Problem Parsing the Package" While Installing Android Application