54

How can I make it so the screen orientation is always landscape?

Do I need to add something to the manifest.xml?

Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
panthro
  • 22,779
  • 66
  • 183
  • 324
  • possible duplicate of [Lock Horizontal View](http://stackoverflow.com/questions/5814960/lock-horizontal-view) – Kerrek SB Dec 07 '11 at 13:25

6 Answers6

101

Add this android:screenOrientation="landscape" to your <activity> tag in the manifest for the specific activity that you want to be in landscape.

Edit:

To toggle the orientation from the Activity code, call setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) other parameters can be found in the Android docs for ActivityInfo.

DRiFTy
  • 11,269
  • 11
  • 61
  • 77
  • 7
    Any way to make this setting application-level? – IgorGanapolsky Dec 23 '13 at 17:10
  • @IgorGanapolsky I assume you mean from the code in your `Activity`? If so, I updated my answer to reflect that with an example. – DRiFTy Dec 23 '13 at 17:48
  • Hi, I meant from the level tag in the Manifest. Like when you specify device compatibility requirements, I was wondering if there was a way to specify landscape-only there as well. – IgorGanapolsky Dec 26 '13 at 17:56
  • @IgorGanapolsky Ohhh gotcha, yeah I do not believe so. This must be specified on a per `Activity` basis at the moment. They could possibly add this as an option in the `android:uiOptions` attribute but I doubt it. – DRiFTy Dec 26 '13 at 19:25
15

Yes, in AndroidManifest.xml, declare your Activity like so: <activity ... android:screenOrientation="landscape" .../>

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
11

Just two steps needed:

  1. Apply setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); after setContentView().

  2. In the AndroidMainfest.xml, put this statement <activity android:name=".YOURCLASSNAME" android:screenOrientation="landscape" />

Hope it helps and happy coding :)

Hoo
  • 1,806
  • 7
  • 33
  • 66
  • 4
    *Note*: `SCREEN_ORIENTATION_LANDSCAPE` and `landscape` switch off the sensor, so it is only *one* of the possible two landscape configurations – there are `SCREEN_ORIENTATION_REVERSE_LANDSCAPE` and `reverseLandscape` as well. If you just want to have a landscape app without forcing the user to hold the device in one of the two ways, use `SCREEN_ORIENTATION_SENSOR_LANDSCAPE` and `sensorLandscape`. [Source](https://developer.android.com/reference/android/R.attr.html#screenOrientation) – Bowi Feb 12 '18 at 08:27
  • 2
    @Bowi Thanks for the SCREEN_ORIENTATION_SENSOR_LANDSCAPE!!! Saved my life (and my monitor's life)! I was really annoyed that I couldn't make it flit only between landscape orientations! – vianna77 Nov 16 '20 at 03:50
9

One thing I've not found through the answers is that there are two possible landscape orientations, and I wanted to let both be available! So android:screenOrientation="landscape" will lock your app only to one of the 2 possibilities, but if you want your app to be limited to both landscape orientations (for them whom is not clear, having device on portrait, one is rotating left and the other one rotating right) this is what is needed:

android:screenOrientation="sensorLandscape"
Pottercomuneo
  • 718
  • 1
  • 6
  • 10
3

When you are in android studio 3 or above you need to add following lines AndroidManifest.xml file

<activity
            android:name=".MainActivity"
            android:configChanges="orientation"
            android:screenOrientation= "sensorLandscape"
            tools:ignore="LockedOrientationActivity">

One thing this is sensor Landscape, means it will work on both landscape sides

But if you only want to work the regular landscape side then, replace sensorLandscape to landscape

SHAH MD IMRAN HOSSAIN
  • 2,558
  • 2
  • 25
  • 44
0

You can try with

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Quick learner
  • 10,632
  • 4
  • 45
  • 55