How do I specify different layouts for portrait and landscape orientations?

177,722

Solution 1

Create a layout-land directory and put the landscape version of your layout XML file in that directory.

Solution 2

You just have to put it under separate folders with different names depending on orientation and resolution, the device will automatically select the right one for its screen settings

More info here:

http://developer.android.com/guide/practices/screens_support.html

under "Resource directory qualifiers for screen size and density"

Solution 3

For Mouse lovers! I say right click on resources folder and Add new resource file, and from Available qualifiers select the orientation :

enter image description here


But still you can do it manually by say, adding the sub-folder "layout-land" to

"Your-Project-Directory\app\src\main\res"

since then any layout.xml file under this sub-folder will only work for landscape mode automatically.

Use "layout-port" for portrait mode.

Solution 4

Just a reminder:

Remove orientation from android:configChanges attribute for the activity in your manifest xml file if you defined it:

android:configChanges="orientation|screenLayout|screenSize"

Solution 5

Fastest way for Android Studio 3.x.x and Android Studio 4.x.x

1.Go to the design tab of the activity layout

2.At the top you should press on the orientation for preview button, there is a option to create a landscape layout (check image), a new folder will be created as your xml layout file for that particular orientation

enter image description here

Share:
177,722

Related videos on Youtube

Jay Askren
Author by

Jay Askren

I'm a java developer with a passion for working with data.

Updated on May 30, 2020

Comments

  • Jay Askren
    Jay Askren almost 4 years

    I've seen references to being able to specify two separate layout xml files for an activity, one for Portrait and one for Landscape. I've not been to find any information on how to do that though. How do I specify for each activity which xml file is it's portrait layout and which is the Landscape layout?

    Is it also possible to specify different layouts for different screen sizes? If so, how is this done?

  • Jay Askren
    Jay Askren over 14 years
    Does that mean all activities need to define both a landscape and a portrait view if I do this?
  • Mark B
    Mark B over 14 years
    No, if no layout-land definition exists it just uses the XML layout in the standard layout directory.
  • SMBiggs
    SMBiggs over 12 years
    Only works for OS versions 1.6 or higher. If you want to support 1.5, you need this link: developer.android.com/guide/practices/screens-support-1.5.ht‌​ml
  • itsaboutcode
    itsaboutcode over 11 years
    @Silvio Donnini - links no longer have the required information.
  • Danation
    Danation over 11 years
    @itsaboutcode Yes it does: Go down to "Using configuration qualifiers"
  • Tushar Pandey
    Tushar Pandey over 10 years
    in my case , layout-land is not working when i am using , android:configChanges="orientation|keyboardHidden|screenSize‌​"
  • Ushal Naidoo
    Ushal Naidoo almost 10 years
    Remove orientation from android:configChanges="orientation|keyboardHidden|screenSize‌​"
  • Smitty-Werben-Jager-Manjenson
    Smitty-Werben-Jager-Manjenson over 5 years
    This worked for me. I added a new layout file as instructed with the same name as the portrait version. Android Studio automatically created a folder for that layout. One layout for portrait and another for landscape. The new layout automatically gets displayed when rotating screen.
  • monK_
    monK_ almost 5 years
    Incorrect. You are not using the folder view mode (like "Project"). You are probably using "Android" view mode, which groups files together like in your screenshot in virtual folders, but the actual folder structure is still how every other post here describes.
  • natansalda
    natansalda about 4 years
    what if I need to have android:configChanges="orientation|screenLayout|screenSize" in my manifest, as some of my fragments can be in both orientation and some don't, but still need separate layouts for landscape and portrait for those fragments that can change orientation dynamically?
  • Nikunjkumar Kapupara
    Nikunjkumar Kapupara over 3 years
    One of the easiest and Shortest way to deal with landscape layout
  • pvd
    pvd over 2 years
    This answer should be chosen
  • Luis A. Florit
    Luis A. Florit over 2 years
    If I remove the orientation from android:configChanges the activity is destroyed when the orientation changes.
  • Luis A. Florit
    Luis A. Florit over 2 years
    When I change the orientation the activity does not switch the layout, staying with the layout when the activity is created. If I remove orientation from android:configChanges, the activity is destroyed.