Android Adding Alternate Layout Resource Folder

Responsive design in android programming should have answer on mobile device orientation change from horizontal to vertical (portrait to landscape) and vice versa. In that sense, it is useful to know android adding alternate layout resource folder. So let’s start with new project in Android studio. On empty project design portrait layout in main activity, by adding background image to content_main.xml and putting four image buttons, like this:

initial-design-for-portrait-mode

Running on real device in portrait mode it looks just fine:

portrait-mode-on-real-device

But on the same device in landscape orientation it looks like this:

landscape-mode-on-real-device

Not so nice, isn’t it? To prevent this we need to make android alternate layout resource folder. In Android studio, just click as it is shown.

Click-for-new-layout-resource

And then choose Create Landscape Variation

Android-adding-alternate-layout-resource-folder

Another path for doing the same thing in Android studio is to go to project mode

Go-to-Project-mode-to-see-new-Layout

Then click to app→src→main, then right click, New→Android resource directory.

New-Android-resource-directory

Then change Resource type to: layout.
Change Available qualifiers to: Orientation.
Click to >>.

Resource-Layout-qualifier-Orientation

And then Screen orientation to: Landscape

Chosen-qualifier-Landscape

In new layout, arrange everything from beginning including background image. Of course, adjust the size of images for image buttons in landscape mode as well.

initial-design-for-landscape-mode

And application now adjust its appearance automatically to portrait or landscape mode.

apropriate-landscape-mode-on-real-device

You can download Android adding alternate layout resource folder project example

External links:

Android adding alternate layout resource folder on Stackoverflow
Android adding alternate layout resource folder on Developer

Posted in android Tagged with: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*