109

Can someone tell me how to lock my application to a portrait mode? Is it a simple configuration in the manifest file?

sexitrainer
  • 1,347
  • 2
  • 12
  • 18
  • Please only do this if you have a big reason for it. It's an antipattern in Android. Users should be able to choose how to use their devices. – Jose Alcérreca Mar 09 '23 at 15:53

4 Answers4

245

Yes. Add android:screenOrientation="portrait" to the manifest under your main activity.

<activity android:name=".yourActivity" android:screenOrientation="portrait"... />
kike
  • 4,255
  • 5
  • 23
  • 41
techi.services
  • 8,473
  • 4
  • 39
  • 42
75

Yes! It's an attribute of the activity tag:

<activity android:name=".yourActivity" android:screenOrientation="portrait" ... />
Francesco Laurita
  • 23,434
  • 8
  • 55
  • 63
12

Also, you may have to add the following to your activity element:

android:configChanges="keyboardHidden"

That way, the OS won't change the orientation when the user opens a sliding keyboard.

Tim Mahoney
  • 680
  • 2
  • 7
  • 15
6

None of these answers worked on my system but I did find the following worked perfectly for a simple app that I developed:

Within MainActivity.java add:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

to onCreate ()

This is mine:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

I know that it is not (always) best practice locking orientation, but in special circumstances it is valid and I only want this temporarily while I continue developing.

Pierre.Vriens
  • 2,117
  • 75
  • 29
  • 42
Ned
  • 61
  • 1
  • 2
  • Welcome to StackOverflow. It is good that you share a solution for your specific case, but you can edit your answer to format properly the code you posted, so it will be more human-readable. To do so, the code and the previous paragraph must be split by at least two linebreaks, and every line in the code block must be indented with at least 4 spaces. **Please try this out** by editing your answer, and you will see by yourself how it makes your answer look much better. – SebasSBM Sep 08 '18 at 10:07
  • You will find complete useful tips to improve answers in general in the section [How do I post a good answer?](https://stackoverflow.com/help/how-to-answer). But, in your case, you will find [this section about markdown in posts](https://stackoverflow.com/help/formatting) much more interesting, I think. Any little improvement in your answer can make a big difference. – SebasSBM Sep 08 '18 at 10:33
  • Thanks for the tips SebasSBM, it looked readable on my screen when I sent it but something was lost on the journey :) – Ned Sep 08 '18 at 11:28