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

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?


Asked by: Audrey606 | Posted: 25-01-2022






Answer 1

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

Answered by: Ted281 | Posted: 26-02-2022



Answer 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"

Answered by: Ryan662 | Posted: 26-02-2022



Answer 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.

Answered by: Kimberly794 | Posted: 26-02-2022



Answer 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"

Answered by: Julian398 | Posted: 26-02-2022



Answer 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

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

Answered by: Maddie427 | Posted: 26-02-2022



Answer 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.

Answered by: Aldus344 | Posted: 26-02-2022



Answer 7

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.

Answered by: Kevin198 | Posted: 26-02-2022



Answer 8

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/

Answered by: Alfred150 | Posted: 26-02-2022



Answer 9

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>

Answered by: Lenny266 | Posted: 26-02-2022



Answer 10

  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

Answered by: Roman459 | Posted: 26-02-2022



Similar questions

android - specify different layouts for portrait and landscape orientations?

I've specify two separate layout xml files for an activity, one for Portrait in res/layout folder and one for Landscape in res/layout-land folder. my actual problem is that when i launch application in portrait mode it takes ui from layout and instantly i rotate my phone it doesn't take xml from layout-land folder it takes ui from layout folder and if i launch my app in landscape mode it takes xml from layout-land folder a...


android - Buttons do different thing for portrait and landscape orientations

In portrait mode,I have an application with three buttons. When the user hits the first button text #1 will appear and if the user hits the second button the text #1 disappears and text #2 appears in the same place, same idea with button three. I'm trying to let the text view appears with an image for each button in only landscape orientation mode. Please can I do that? see my codes below. Thanks! XML:


android - Square splash screen that look the same in both portrait and landscape device orientations

I want to use a nine-patch image to make a splash screen for my application, which should look the same (square) in both portrait and landscape orientation. I tried to use a nine-path image for it here, but was answered that nine-patch images are not designed for this. The code is simple:...


java - Android - portrait and landscape orientations?

I am learning android development and I have made a calculator app for practice. It works fine in portrait mode. So I made a new xml file for landscape after the app is not even working in portrait mode. As soon as it starts, it crashes with the message "Unfortunately app has stopped working". Here is my code AndroidManifest.xml &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android...


android - specify different layouts for portrait and landscape orientations?

I've specify two separate layout xml files for an activity, one for Portrait in res/layout folder and one for Landscape in res/layout-land folder. my actual problem is that when i launch application in portrait mode it takes ui from layout and instantly i rotate my phone it doesn't take xml from layout-land folder it takes ui from layout folder and if i launch my app in landscape mode it takes xml from layout-land folder a...


android - Buttons do different thing for portrait and landscape orientations

In portrait mode,I have an application with three buttons. When the user hits the first button text #1 will appear and if the user hits the second button the text #1 disappears and text #2 appears in the same place, same idea with button three. I'm trying to let the text view appears with an image for each button in only landscape orientation mode. Please can I do that? see my codes below. Thanks! XML:


android - Square splash screen that look the same in both portrait and landscape device orientations

I want to use a nine-patch image to make a splash screen for my application, which should look the same (square) in both portrait and landscape orientation. I tried to use a nine-path image for it here, but was answered that nine-patch images are not designed for this. The code is simple:...


java - Android - portrait and landscape orientations?

I am learning android development and I have made a calculator app for practice. It works fine in portrait mode. So I made a new xml file for landscape after the app is not even working in portrait mode. As soon as it starts, it crashes with the message "Unfortunately app has stopped working". Here is my code AndroidManifest.xml &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android...


kotlin - How do I allow only one specific fragment to have both portrait and landscape orientations in Android?

In my Android application, I have added android:screenOrientation=&quot;portrait&quot; to all activities in my Android.manifest and android:orientation=&quot;vertical&quot; to every activity's/fragment's respective layout so that the app can only be viewed in portrait mode. However, there is one fragment in particular that I want to enable both portrait and landscape orientatio...


java - How can I save state between orientations?

In a previous question I asked how I could display two different layouts for portrait and landscape orientations: Problem switching to landscape orientation Now I would like to know how I can save the state of my application before it goes into another orientation? I seem to lose this data any time orie...


android - Is it possible to support both orientations on some screen sizes but not others?

In my apps I currently only support the portrait orientation for some activities, because I assume that most phone users will be using portrait orientation (and holding the device with one hand) most of the time. On the new Honeycomb tablets, however, it seems likely that users will be using the landscape orientation much more often, so I'd like to support both orientations for more of my activities. I'd ra...


How do I switch orientations in android faster?

I have an android app that has to load very large files on start up. any time it switches orientations, it has to destroy and recreate the activity, causing the large files to reload. this takes about 30 seconds, this is pretty inconvenient if I were to actually publish this. I am sure that I am doing something wrong here. I have heard of onSaveInstanceState() and onRestoreInstanceState(), but I have no idea how to use the...


layout - Android: How to allow orientations based on device

I am developing an Android application and I'd like to support all device sizes, from phones to full-size tablets. I'd like to restrict the application to only using portrait orientation on smaller devices and allow a different, more large-screen friendly UI to be used in landscape when the device is larger. So far, I am unable to find any way, let alone a graceful way, to restrict an activity to allowing orientations base...


android - Checkboxes all showing the same text after switching orientations

Edit: See the accepted answer. Lesson: Sometimes views will save and restore their state automatically. This happens AFTER onCreate. This can cause the overwriting of stuff you did in onCreate. If you don't have unique ids, all views of a certain kind (in my case textboxes) can be overwritten with the same saved state. (ps: thanks for your help everyone!) So, I have a simple linear layout and I...


android providing different drawable resources for orientations

I would like to provide different drawable resources for different orientations. What is the proper folder name to provide a landscape and portrait version of a drawable? I looked through the android documentation and haven't seen anything like drawable-hdpi-land or drawable-hdpi-port. Are these the proper names?


android - Supporting both landscape orientations in Honeycomb

Is it possible to support both landscape orientations in Honeycomb? My application is set to landscape orientation only, but it doesn't change if you flip the device 180 degrees.


android - how to maintain unique size of a polygon in both orientations?

i am rendering a cube using openGLES1.0.if i change orientation(Landscape to portrait and viceversa)of device,cube size is changing.in portrait mode,cube size is big,where as in Landscape orientation size became low.i want to make it of unique size in both orientations.please help me.Thanks in advance


Android camera preview works only in 2 out of 3 phone orientations

In my Android App I am using the Camera class to grab snapshots from the phone's camrea. I use startPreview to start the camera preview and then takePicture to grab images. I am forcing landscape on the App by adding android:screenOrientation="landscape" To AndroidManifest.xml. The problem is when I return to my App when my phone is rotated 90 degrees counter clockwise. In this case, I ca...


java - how to get android camera to work in multiple orientations

OK I have this camera code I want to make it so that it will work in all orientations. and on android 2.2/2.3.3/3.2 I was thinking something like if(Build.VERSION.SDK_INT == 8) do v8 work else if(Build.VERSION.SDK_INT == 10) do v10 work else if(Build.VERSION.SDK_INT == 14) do v14 work but I'm not having any luck. Everything I try fails. Please help. Activity.java






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top