How to disable landscape mode/orientation for an Activity? (Android Studio) [Solution]

It is very easy to disable the landscape mode for your activity.
You can do it in two ways:

  • Method #1: Just open your AndroidManifest.xml file and add the following code to the activity or activities for which you want to disable the landscape orientation:
android:orientation=portrait

However, I would suggest you use the following instead:

android:orientation=sensorPortrait

The latter enables the screen to be rotated when the device is flipped upside down (very common with tablet users).

Disabling Landscape Mode using XML
Disabling Landscape Mode using XML
    • Method #2: Alternatively you can do it with Java too (programatically). Just add the following code to the OnCreate Method of your Activity Class:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

so that the OnCreate method looks like this:

@Override
public void onCreate(Bundle savedInstanceState) {
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Disabling Landscape Mode using JAVA(Prrogramatically)
Disabling Landscape Mode using JAVA(Prrogramatically)

To disable landscape orientation on all the Activities just follow any of the methods above for all the activities.
However, there is a rather simple way to disable landscape Orientation for the whole Application(for all the Activities).
I found it on StackOverflow.

All you need to do is make an Abstract Activity that all your Activities extend.

public abstract class AbstractActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

Well that’s it!!
I hope this tutorial helped you.
If you have any question or doubt just leave it in the comments section down below.

[wpedon id="7041" align="center"]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.