I have created various layout resource files in order to ensure that my app supports all screen and devices sizes.
My problem is which one of the below is suitable to use in my AndroidManifest file. Once the user switches the device to landscape I would like Android to choose the landscape resource file instead of portrait.
android:configChanges="screenSize|keyboardHidden"
android:configChanges="orientation"
Which one is more suitable to use?.
You just create One extra folder in your res folder named layout-land and put all landscape layouts in this folder then operating system automatic show the layouts according device orientation.
No need to add anything Manifest File.
Android system does it automatically. You don't need to put anything in your manifest.
I have divided my layout resources in Android project to many folders such asL
layout, layout-large, layout-normal-port-xhdpi-800x480, and so on.
The problem is when I click on xml file inside the (layout) folder which is the default folder it always opens in landscape; I tried to switch to portrait but it opens the xml file inside (layout-normal-port-xhdpi-800x480) folder.
How can I view the default layout as a portrait not landscape?
Appreciate your help.
I would suggest using the new layout qualifiers.
See here: Support Multiple Screens
layout-sw600dp (7Inch Tablets)
layout-sw720dp (10Inch Tablets)
etc.
They're all listed there.
I have read lots of post on this forum and others about using a different layout depending on the device being used.
Below is a screenshot of my layout folder at the moment:
Now, I have tried many different variations to try and get the XML layout to change. The project is designed using a phone and I'm now testing on a 10.1 inch Samsung Galaxy tablet.
Despite all those layouts my app still uses the default .xml file.
Any ideas where I'm going wrong or other variations to try?
I should say I lock the user into portrait mode hence a few layouts with -port.
Any help would be great
thanks
You need to have your layout-large, layout-sw400dp-port,... folders at the same level as your layout folder instead of having them inside the layout folder.
Your folder structure should look like -
/res/layout/
/res/layout-large/
/res/layout-sw400dp-port/main.xml
...
I know you can specify a different layout file for orientation by creating a directory;
layout-land
And I know you can create another layout file for a large(tablet) screen size by creating a directory;
layout-large
My question is, can you create a directory that supports both? Something like;
layout-land-large
It's vital for a piece of work that a certain screen layout is displayed only on a tablet device in landscape mode, otherwise it's the default. If my solution isn't valid, is there another way to do this?
Thanks.
Change the folder name to: layout-large-land
This link explains how provide resources for different configurations:
http://developer.android.com/guide/topics/resources/providing-resources.html
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?
Create a layout-land directory and put the landscape version of your layout XML file in that directory.
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"
For Mouse lovers! I say right click on resources folder and Add new resource file, and from Available qualifiers select the orientation :
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.
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"
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
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).
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.
I hope this works for you.
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.
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/
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>
Right click res folder,
New -> Android Resource File
in Available qualifiers, select Orientation,
add to Chosen qualifier
in Screen orientation, select Landscape
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.
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.
Source: https://stackoverflow.com/a/74047314/3718756