Android: alternate layout xml for landscape mode

181,737

Solution 1

By default, the layouts in /res/layout are applied to both portrait and landscape.

If you have for example

/res/layout/main.xml

you can add a new folder /res/layout-land, copy main.xml into it and make the needed adjustments.

orientation

See also http://www.androidpeople.com/android-portrait-amp-landscape-differeent-layouts and http://www.devx.com/wireless/Article/40792/1954 for some more options.

Solution 2

In the current version of Android Studio (v1.0.2) you can simply add a landscape layout by clicking on the button in the visual editor shown in the screenshot below. Select "Create Landscape Variation"

Android Studio add landscape layout

Solution 3

The layouts in /res/layout are applied to both portrait and landscape, unless you specify otherwise. Let’s assume we have /res/layout/home.xml for our homepage and we want it to look differently in the 2 layout types.

  1. create folder /res/layout-land (here you will keep your landscape adjusted layouts)
  2. copy home.xml there
  3. make necessary changes to it

Source

Solution 4

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

Solution 5

You can group your specific layout under the correct folder structure as follows.

layout-land-target_version

ie

layout-land-19 // target KitKat

likewise you can create your layouts.

Share:
181,737
700 Software
Author by

700 Software

Join 700.social ! A happy medium between Facebook and Gab. :) The name is too long but the domain looks good. Also, Software Development / Consulting (423) 802-8971 700software.com old username: George Bailey (but now I use my real name) http://www.google.com/images?q=George+Bailey

Updated on November 19, 2021

Comments

  • 700 Software
    700 Software over 2 years

    How can I have one layout for landscape and one for portrait? I want to assume extra width and conserve vertical space when the user rotates the phone over sideways.

  • Vamsi Pavan Mahesh
    Vamsi Pavan Mahesh over 10 years
    should i have to keep the name layout-land or any other word @marapet
  • Foo
    Foo about 10 years
    You can't use any other word
  • Azurespot
    Azurespot about 9 years
    Except it puts a new copy in your layout-land folder. Any idea how to call a layout from there? Can't use R.layout.layout_name. I'm trying to configure my own layouts manually upon configuration change, thanks.
  • Distwo
    Distwo almost 9 years
    @NoniA. It should detect when the phone switch to landscape and call the one from layout-land automatically.
  • Prabs
    Prabs almost 9 years
    My background image stretches out on landscape.I have layout-land and drawable-land..still it stretches out..I've tried using same image,9-patch images,mdpi,hdpi n all..still issue persists :(
  • dsdsdsdsd
    dsdsdsdsd almost 8 years
    what is the word for portrait
  • dsdsdsdsd
    dsdsdsdsd almost 8 years
    what about for 'portrait'?
  • Ruchir Baronia
    Ruchir Baronia over 7 years
    What about with the new <sw> qualifiers?
  • 700 Software
    700 Software over 7 years
    Thanks for answering, but I don't see how your answer helps because this question was already answered. Maybe you could explain the benefit of the -19 suffix? Is that helpful in any way?
  • Gregzenegair
    Gregzenegair over 7 years
    @dsdsdsdsd according to Android studio it is layout-port
  • MindRoasterMir
    MindRoasterMir about 5 years
    after i create layout-land, it was invisible then i had to right click on the layout folder and select show in folder then i copied and pasted the <layout>.xml file there. then it appered in the Project explorer. thanks
  • jimmy
    jimmy over 2 years
    @Distwo but the problem is, if app is running in portrait mode it will shut off, i have to keep using it in landscape to word, because there some elements I used in MainActivity.java when the class is created. People who actually made this Android are dumb founded, seriously
  • Distwo
    Distwo over 2 years
    You need to architect your view and models properly. Here is an article about preserving data across configuration changes. developer.android.com/guide/topics/resources/runtime-changes