How to Disable Screen Orientation Change in Android Programmatically

Android phone and tablets have the option to ON/OFF auto rotation of the screen to portrait or landscape mode. We developer can disable auto-rotate the screen in our android application programmatically or can choose only portrait or landscape mode. In this tutorial, you will learn to change or disable android screen orientation change/rotate programmatically.

There are lots of ways to change or disable screen rotation programmatically in android and here I am going to show how to disable screen orientation with different method using java code and android manifest file.

How to Check Android Device Screen Orientation
Turn ON and OFF WiFi Connection Programmatically in Android
How to Launch Other Application From Your Android App Programmatically

Android Example: Change Android Screen Rotation Programmatically

Android Example: How to Change Android Screen Rotation Programmatically

Following are the different methods to change android screen orientation to portrait or landscape mode.

Method 1: Using Java Code

Add setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); to run your application in PORTRAIT mode and setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); to run in LANDSCAPE mode before setContentView of your java activity onCreate method. Your activity file will look like this.

src/ DisableScreenRotationAndroid.java

Method 2: Using AndroidManifest.xml File

You can also disable or change screen orientation by adding android:screenOrientation="portrait" for portrait mode and android:screenOrientation="landscape" for landscape mode in your project AndroidManifest.xml file which is looks like this.


These are the best and easiest methods to change the screen rotation in your android application programmatically. You can find some other ways too.
Tutorial 8001417919207622172
Home item