How can I disable landscape mode in Android?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Disabling landscape mode in Android usually means locking one activity, or the whole app, to portrait orientation. The simplest solution is a manifest setting, but the right choice depends on whether the orientation should always be fixed or whether it changes based on runtime conditions.
Lock an Activity in the Manifest
For most apps, the cleanest solution is to declare the activity orientation in AndroidManifest.xml.
This tells Android that the activity should stay in portrait mode even if the user rotates the device. It is the least error-prone option when the screen should never support landscape.
If you want to apply the rule to several activities, add the attribute to each activity explicitly rather than relying on ad hoc code in every onCreate method.
Lock Orientation Programmatically
Sometimes orientation should depend on runtime conditions, such as a feature flag or one special workflow. In that case, set the requested orientation in code.
This works, but it is usually less transparent than a manifest entry. Prefer code only when the choice genuinely depends on runtime state.
Know the Difference Between portrait and Related Modes
Android offers several portrait-oriented options. portrait is strict and straightforward. Other modes such as sensorPortrait allow upside-down portrait on devices that support it.
Use the strict mode when you want predictable behavior and the UI does not need orientation flexibility. Use other orientation constants only if the distinction matters to the app experience.
Avoid Treating configChanges as the Fix
Some developers try to solve rotation by declaring configChanges and manually handling orientation changes. That is not the same as disabling landscape mode.
This tells Android that the activity will handle certain configuration changes itself, but it does not prevent the device from rotating into landscape. If the real goal is "never allow landscape," set the screen orientation instead.
Test on Devices with Different Behavior
Orientation handling is affected by form factor and system behavior. Phones, tablets, foldables, and multi-window environments can surface differences you may not notice on one emulator.
At minimum, test:
- a normal phone in portrait and rotation attempts
- a tablet or large-screen emulator
- configuration changes such as split-screen if your app supports them
Locking orientation is easy to declare, but the surrounding UI behavior still deserves testing.
Think About User Experience Before Locking
Some apps genuinely should not support landscape because the layout is designed for one orientation only. Others lock portrait out of habit and accidentally hurt usability on larger screens or accessibility setups.
A good engineering question is not only "how do I lock orientation," but also "should this screen really be locked." If the feature benefits from wider layout, input devices, or media consumption in landscape, forcing portrait may be the wrong product decision.
Common Pitfalls
- Using programmatic locking everywhere when a manifest entry would be simpler makes the app harder to maintain.
- Assuming
configChangesdisables rotation misunderstands what that setting actually controls. - Locking the entire app without checking tablet or large-screen behavior can create poor UX.
- Testing only on one emulator misses device-specific orientation behavior.
- Treating portrait lock as a design default instead of a deliberate UX choice leads to unnecessary restrictions.
Summary
- The simplest way to disable landscape mode is
android:screenOrientation="portrait"in the manifest. - Programmatic orientation locking is useful only when runtime conditions actually matter.
- '
configChangesdoes not disable landscape mode.' - Test locked-orientation screens on more than one device shape.
- Decide based on product and UX needs, not just implementation convenience.
Related reading
- How can I disable landscape mode in Android?
- How can I disable the UITableView selection?
- How can I disable the UITableView selection?
- How can I display a list view in an Android Alert Dialog?
- How can I do Key Value Observing and get a KVO callback on a UIView's frame?
- How can I easily delete all objects in a Realm
- How can I enable zoom in on UIWebView which inside the UIScrollView?
- How can I encode/decode a string to Base64 in Swift?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.