How to disable landscape mode in Android?


Android supports two orientations as portrait and landscape. we can disable orientation in android application. This example demonstrate about how to disable landscape mode in Android.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   xmlns:tools = "http://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:background = "#dde4dd">
   <EditText
      android:id = "@+id/editText"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content" />
</LinearLayout>

In the above code, it contains linear layout and editext. it going to support both landscape and portrait as shown below -

The above output indicates about landscape mode

The above output indicates about portrait mode.

To disable landscape mode need to add screenOrientation tag in manifest.xml as shown below -

<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.example.andy.myapplication">
   <application
      android:allowBackup = "true"
      android:icon = "@mipmap/ic_launcher"
      android:label = "@string/app_name"
      android:roundIcon = "@mipmap/ic_launcher_round"
      android:supportsRtl = "true"
      android:theme = "@style/AppTheme">
      <activity android:name = ".MainActivity"
         android:screenOrientation = "portrait">
         <intent-filter>
            <action android:name = "android.intent.action.MAIN" />
            <category android:name = "android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

In the above code we have given screenOrientation as portrait means MainActivity going to support only portrait mode only . If you want to develop an application in portrait mode, add screenOrientation inside application tag.

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

In the above result it is showing only portrait mode. now turn your device it not going to change the view according to orientation.

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements