How to Disable Landscape Mode in Android

How to Disable Landscape Mode in Android

22 May 2024 Stephan Petzl Leave a comment Tech-Help

Disabling landscape mode for specific views in your Android application can improve user experience by maintaining a consistent orientation. This guide provides actionable steps to achieve this, using both XML configurations and Java code.

Method 1: Using AndroidManifest.xml

The most straightforward way to disable landscape mode is by modifying your AndroidManifest.xml file. This method ensures that your activity remains in portrait mode regardless of device rotation.

        <activity android:name=".YourActivity"
                  android:screenOrientation="portrait" />

By adding android:screenOrientation="portrait" to your activity tag, you can lock the orientation to portrait mode.

Method 2: Programmatic Approach

If you prefer to control the orientation programmatically, you can use the following Java code within your activity:

        protected void onCreate(Bundle savedInstanceState) {

This approach is useful when you need to dynamically change the orientation based on specific conditions within your application.


While forcing portrait orientation can be beneficial, it’s essential to consider the following:

  • Ensure you handle activity lifecycle events correctly, as forcing orientation does not eliminate the need to manage state changes.
  • Consider the user experience on devices with different hardware configurations, such as tablets or devices with hardware keyboards.
  • Use sensorPortrait instead of portrait to allow upside-down portrait mode, which is common on tablets.

Advanced Techniques

For more advanced control over orientation, you can use nosensor to ignore sensor data or handle orientation changes within specific views programmatically. For instance:

        public class OrientationUtils {
            public static void lockOrientationPortrait(Activity activity) {

            public static void unlockOrientation(Activity activity) {

Using utility classes like OrientationUtils can help manage orientation changes dynamically.

Enhance Your Testing with Repeato

Ensuring your app’s orientation behaves as expected across various devices can be challenging. Repeato, a no-code test automation tool for iOS and Android, simplifies this process. It allows you to create, run, and maintain automated tests effortlessly, focusing on delivering a great product rather than managing tests. Repeato’s computer vision and AI capabilities make it particularly fast and efficient, enabling even non-technical colleagues to contribute to test automation.

For more information on optimizing your testing workflow, check out our documentation on virtual test devices and continuous integration.

Like this article? there’s more where that came from!