Android Orientation


Based on content of the Activity we may require to start our App, either in Landscape (Horizontal) or Portrait (Vertical) mode. This can be done in two ways –

  1. Adding android orientation in Manifest.xml file.
  2. Adding orientation from file.

In this tutorial you will study both approaches of android orientation. You will also learn to develop separate custom layout for both Landscape-Portrait mode.


Important Tip : Our Activity is destroyed & recreated, every time we change screen orientation. So keep a practice of saving all variable, temp. data, preferences before change of Orientation. For this override onDestroy() method & inside it save all your valuable data.


Source Code :


Now Let us work on Project : (Refer – Configuring Existing AndroidStudioProject) Orientation which you might have downloaded from above or you can follow the scripts below by copying it to implement  in your Project. To launch Downloaded project, Extract the zip, copy it into AndroidStudioProjects. Now from Android Studio click open Existing Android Studio Project and select the path to extracted file we just copied in AndroidStudioProjects directory. If Sync SDK path prompt comes just click OK and you are ready to work. If still you get error check for AndroidStudioProjects/Orientaion/app/build.gradle , it will contain different SDK Version. Change it to SDK Version which you have.



1. AndroidManifest.xml 

  •  Line 12  Inside the Activity TAG of MainActivity we add a property – android:screenOrientation=”landscape”. By doing this our MainActivity will start in Landscape mode by default. This is our 1st way, where we statically mention which mode to be launched.

Now we will see the 2nd way for changing orientation from file, where we can change orientation on runtime also.




Explaining above Code :

  • Line 21  whenever we click on our button, it will invoke  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  which will turn screen orientation from Portrait mode to Landscape Mode.
  • Line 29  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  will turn screen orientation from Landscape mode to Portrait Mode


You should check the below video for complete understanding on changing Android Screen Orientation (Make sure you switch to project view from Package explorer in left pane) :


Developing Separate XML Layout file for Landscape – Portrait mode :

Android Orientation

Now let us see developing 2 separate files for landscape & portrait mode. Our 1st activity_main.xml file inside layout folder is for portrait mode. And 2nd activity_main.xml file inside layout-land folder for Landscape mode. 


3(a).  activity_main.xml   >  for Portrait Mode

  • We have simply added 1-button with its onclick-handler method which will be used to change screen orientation to Landscape mode.
  • This file will be loaded when we are in portrait mode. It may also be loaded in Landscape mode if we have not created a separate file inside layout-land folder supporting landscape mode.


3(b).  activity_main.xml   >  for  Landscape mode

  • To develop App. for Landscape mode create folder layout-land inside res folder & place your activity_main.xml (here we use same name of layout file) inside it. It is optimal to use separate file for Landscape mode, yet this is not compulsory the device will still load the default activity_main.xml which we made for portrait mode.

  •  We have simply added 1-button with its onclick-handler method which will be used to change screen orientation to Portrait mode.



Final Output :

Android Orientation Landscape Mode

Android Orientation – Landscape Mode


Android Orientation Portrait Mode

Android Orientation – Portrait Mode



Do It Yourself :

  • Remove the layout-land folder & the activity_main.xml inside it. Despite launching your application in landscape mode, android system will automatically load the normal activity_main.xml which we developed for portrait mode.



Report Errors + Bugs & Become Insider for

We would like to hear you, if you find any error or misspelled phrase while reading our tutorials. By reporting mistakes through email to you could help other peers.