How can I make it so the screen orientation is always landscape?
Do I need to add something to the manifest.xml
?
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
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.
Yes, in AndroidManifest.xml
, declare your Activity
like so: <activity ... android:screenOrientation="landscape" .../>
You can try with
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
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"
Just two steps needed:
Apply setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
after setContentView().
In the AndroidMainfest.xml, put this statement <activity android:name=".YOURCLASSNAME" android:screenOrientation="landscape" />
Hope it helps and happy coding :)