How can I disable landscape mode in Android?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Sometimes, a mobile app may need to support only portrait mode to deliver a more focused and controlled user experience. Disabling landscape mode in Android applications can ensure consistency in the user interface, particularly when dealing with design elements that are best viewed or interacted with in portrait orientation. This article details different methods to effectively disable landscape mode in Android applications, covering XML, programmatic approaches, and more advanced configurations.
Disabling Landscape Mode Using AndroidManifest.xml
The most straightforward approach to locking your Android app in portrait mode is by modifying the AndroidManifest.xml file. By setting the screenOrientation attribute of your activity to "portrait", you prevent the activity from rotating to landscape mode.
Example:
Explanation:
android:screenOrientation: This attribute controls the orientation in which an activity is displayed. By setting it to"portrait", it locks the activity in portrait mode.
Different options for android:screenOrientation include:
landscape: Locks the activity in landscape mode.sensor: Allows the activity to change orientation based on sensor data.user: Allows the user to dictate the orientation based on their device settings.
Programmatically Controlling Orientation
In certain cases, you may want to control the orientation more dynamically. You can do this using Java or Kotlin within your activity.
Java Implementation:
Kotlin Implementation:
Explanation:
setRequestedOrientation: This method dynamically sets the orientation of the current activity.ActivityInfo.SCREEN_ORIENTATION_PORTRAITenforces the portrait mode for this particular activity during runtime.
Using Configuration Changes
Another approach involves handling configuration changes when the orientation changes. This method allows more control by explicitly handling what occurs when orientation changes are detected.
Example:
Implementation in Java:
Kotlin Implementation:
Explanation:
android:configChanges: This attribute tells the system to deliver configuration changes, such as orientation changes, directly to the activity without restarting it.onConfigurationChanged: This method handles any configuration changes that occur. Here, we enforce portrait mode when a landscape orientation is detected.
Advanced Considerations and Best Practices
Locking the orientation might affect user experience:
- User Control: Allow users to choose their preferred orientation when possible, unless it's critical for app functionality.
- Multi-Activity Apps: If your app comprises multiple activities, ensure that each relevant activity has orientation settings appropriately configured.
- Handle UI Adjustments: Verify that forcing orientation does not compromise UI layouts or usability. Always test on devices with different screen sizes.
Summary Table
Several methods can be implemented to disable landscape mode in Android applications:
| Method | Description | Use Case |
| XML Attribute | Set android:screenOrientation to portrait in AndroidManifest.xml for static enforcement. | Simple apps with one orientation regardless of condition. |
| Programmatically | Use setRequestedOrientation in onCreate. | Dynamic control, useful when orientation needs might change at runtime. |
| Configuration Changes | Override onConfigurationChanged to manage orientation changes. | Applications needing custom handling of configuration changes. |
Using any of these methods will help maintain a consistent user interface by disabling landscape mode in Android applications. Always evaluate the needs of your users and application design before implementing these changes.

