Android: alternate layout xml for landscape mode
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.
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"
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.
- create folder /res/layout-land (here you will keep your landscape adjusted layouts)
- copy home.xml there
- make necessary changes to it
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
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.
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, 2021Comments
-
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 over 10 yearsshould i have to keep the name layout-land or any other word @marapet
-
Foo about 10 yearsYou can't use any other word
-
Azurespot about 9 yearsExcept it puts a new copy in your
layout-land
folder. Any idea how to call a layout from there? Can't useR.layout.layout_name
. I'm trying to configure my own layouts manually upon configuration change, thanks. -
Distwo almost 9 years@NoniA. It should detect when the phone switch to landscape and call the one from layout-land automatically.
-
Prabs almost 9 yearsMy 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 almost 8 yearswhat is the word for
portrait
-
dsdsdsdsd almost 8 yearswhat about for 'portrait'?
-
Ruchir Baronia over 7 yearsWhat about with the new <sw> qualifiers?
-
700 Software over 7 yearsThanks 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 over 7 years@dsdsdsdsd according to Android studio it is layout-port
-
MindRoasterMir about 5 yearsafter 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 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 over 2 yearsYou 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