Android Studio: multiple layouts, specifically landscape tablet - android

I am creating an application for a school project within Android Studio. I am required to make the application run on both phones and tablets in both landscape and portrait modes. I have created 3 different layouts for my homepage by opening the Design View and selecting Create XXXX Variation from the Orientation For Preview Section. So now I have a variation for portrait phone, landscape phone, and tablet but I still need one for landscape tablet. I noticed there is a Create Other Variation but I am not exactly sure how I would go about setting that to landscape tablet. Any advice is appreciated
For reference, here is what my layout folder looks like with my different variations.

Click Create Other.. Select Orientation from Available Qualifiers box. Click >> button. In Screen Orientation select Landscape. In directory name paste this : layout-sw600dp-land
A new folder will be created with the same name having activity_main.xml in sw600dp(tablet 7" size) in landscape mode. Configure as per your requirement.

For a tablet you might use resource directory large, xlarge and large-land, xlarge-land.

Related

How to use tablet-specific layout when in landscape mode?

I have defined a tablet-specific layout in the res/layout-sw600dp folder. Now. It loads correctly in tablet emulator and is checked and managed by code as expected. The problem is that I cannot access this layout variant to use in mobile emulator after I check that orientation is landscape. When I type in code R.layout., no hint is given as to the specific tablet layout, only one instance is given, activity_main. How can I use that tablet layout in the landscape orientation as well?
./app/src/main/res/layout-sw600dp/activity_filter.xml
./app/src/main/res/layout/activity_filter.xml
The "smallest width" qualifier doesn't care about device orientation. You'll have to make a copy of your layout and put it in the res/layout-land/ directory if you want that same layout on phones in landscape.
If you don't want to have two exact duplicate layouts (one in sw600dp and one in land), you can look into using a resource alias: https://developer.android.com/training/multiscreen/screensizes.html#TaskUseAliasFilters

Seek for a better way to set different layouts for phone and tablet in landscape mode

In my android app, I want to load different layouts for phone and tablet in landscape mode, the layout in tablet landscape is the same as portrait in both.
So my layout folder in res is like layout(default), layout-land(for phone landscape), layout-sw600dp-land(for tablet landscape). But layout(default) and layout-sw600dp-land(for tablet landscape) is totally the same. I want to optimize this, any help is appreciated.
I plan to use layout alias, but I cannot understand the sample code in website, what I doubt is if I need to load the layout in the code, which xml file should I use?

Android Layout Folder for Devices/Tablets which are always in Landscape

I'm developing an android app for phones and tablets.
I want all of the devices to use portrait layout. I made a 'layout' folder for it. But, there are some X number of devices which are always in landscape mode. Probably, large tablets. So, I made a 'layout-land' folder too.
Which folder I need to add so that X amount devices will always display landscape layout only, while other devices will always display portrait layout?
Main requirement is to have only portrait layouts displayed. But, display Landscape layout when device itself is in landscape mode(default). I am talking about devices that display drawer in landscape mode, and the ones that open landscape layout of apps only.
Is this automatic?
I have android:screenOrientation="portrait" in manifest.
I can't test this as I don't have a tablet/large device.
I have already looked at this question and other similar questions.
Add the below line in your onCreate() method before setContentView()
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORT‌​RAIT);

How to handle size and position of controls in Android's Activity for portrait and landscape orientation?

I want to desing a GUI for android's tablet. By default i want to use landscape orientation of this application, so i placed the controls (text boxes, text views etc) accordingly, but when i change the orientation of tablet from landscape to portrait, whole GUI of activity gets distrubed. Can anybody guide/help me what is the best approach for designing of GUI in android such that controls appearance should remain correct in either orientation (portrait or landscape)? Thanks in advance.
You have to create another folder in the res directory called "layout-land". Put the layout file for landscape in this folder and the one for protrait mode in the normal "layout" folder. Both files must have the same name. Android will automatically chose the correct layout depending of the current orientation of the device.
Check the link below, it will provide all information about how to design an application to support multiple screens with different sizes and resolutions.
Supporting multiple screens

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?
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

Categories

Resources