162

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?

11 Answers 11

222

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

5
  • 5
    Does that mean all activities need to define both a landscape and a portrait view if I do this?
    – Jay Askren
    Commented Jan 23, 2010 at 19:01
  • 21
    No, if no layout-land definition exists it just uses the XML layout in the standard layout directory.
    – Mark B
    Commented Jan 23, 2010 at 19:49
  • in my case , layout-land is not working when i am using , android:configChanges="orientation|keyboardHidden|screenSize" Commented Feb 11, 2014 at 6:53
  • 10
    Remove orientation from android:configChanges="orientation|keyboardHidden|screenSize" Commented Jun 4, 2014 at 21:59
  • 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. Commented Jan 26, 2022 at 14:30
64

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"

3
30

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.

1
  • 1
    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. Commented Jan 22, 2019 at 21:00
25

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

Update: On newer versions the options have been moved to the context menu of the .xml filename button. (Big thanks to Oliver Hoffmann for pointing that out).

enter image description here

3
  • One of the easiest and Shortest way to deal with landscape layout Commented Jan 1, 2021 at 7:51
  • This answer should be chosen
    – pvd
    Commented Oct 21, 2021 at 2:51
  • 1
    On newer AndroidStudio versions this option moved a bit to the context menu of the .xml filename button Commented Aug 4, 2022 at 9:31
22

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"
2
  • 2
    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?
    – natansalda
    Commented Feb 20, 2020 at 13:57
  • If I remove the orientation from android:configChanges the activity is destroyed when the orientation changes. Commented Jan 26, 2022 at 14:21
6

I think the easiest way in the latest Android versions is by going to Design mode of an XML (not Text).

Then from the menu, select option - Create Landscape Variation. This will create a landscape xml without any hassle in a few seconds. The latest Android Studio version allows you to create a landscape view right away.

enter image description here

I hope this works for you.

4

Create a new directory layout-land, then create xml file with same name in layout-land as it was layout directory and align there your content for Landscape mode.

Note that id of content in both xml is same.

4

The last line below is an example for applying two quantifiers: landscape and smallest width(600dp) screen. Update 600dp with the ones you need.

res/layout/main_activity.xml                # For handsets
res/layout-land/main_activity.xml           # For handsets in landscape
res/layout-sw600dp/main_activity.xml        # For 7” tablets
res/layout-sw600dp-land/main_activity.xml   # For 7” tablets in landscape

The above applies to dimens as well

res/values/dimens.xml                # For handsets
res/values-land/dimens.xml           # For handsets in landscape
res/values-sw600dp/dimens.xml        # For 7” tablets
res/values-sw600dp-land/dimens.xml   # For 7” tablets in landscape

A useful device metrics: https://material.io/tools/devices/

2

Or use this:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:scrollbars="vertical" 
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent">

  <LinearLayout android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

     <!-- Add your UI elements inside the inner most linear layout -->

  </LinearLayout>
</ScrollView>
2
  1. Right click res folder,
  2. New -> Android Resource File
  3. in Available qualifiers, select Orientation,
  4. add to Chosen qualifier
  5. in Screen orientation, select Landscape
  6. Press OK

Using Android Studio 3.4.1, it no longer creates layout-land folder. It will create a folder and put two layout files together.

enter image description here

1
  • 3
    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.
    – monK_
    Commented Jun 14, 2019 at 6:14
2

In Android Studio Chipmunk, the options you are looking for are here.

For landscape, the option is "Create Landscape Qualifier" in the XML name drop-down.
For different screen sizes, the option is "Create Tablet Qualifier"

these have been moved from previous Android Studio versions.

enter image description here

Source: https://stackoverflow.com/a/74047314/3718756

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.