How to set different layouts for Portrait and Landscape in Android?

By | August 20, 2012

This simple example shows how to set different layouts for portrait and Landscape in Android.

If you need a detailed explanation please check this url

http://developer.android.com/guide/practices/screens_support.html

Android Supports these different categories of screens.

1. xlarge screens are at least 960dp x 720dp

2. large screens are at least 640dp x 480dp

3. normal screens are at least 470dp x 320dp

4. small screens are at least 426dp x 320dp

We use configuration qualifier for doing this.

To use a configuration qualifier:

  1. Create a new directory in your project’s res/ directory and name it using the format:<resources_name>-<qualifier>
    • <resources_name> is the standard resource name (such as drawable or layout).
    • <qualifier> is a configuration qualifier from table 1, below, specifying the screen configuration for which these resources are to be used (such as hdpi or xlarge).

    You can use more than one <qualifier> at a time—simply separate each qualifier with a dash.

  2. Save the appropriate configuration-specific resources in this new directory. The resource files must be named exactly the same as the default resource files.

For example, xlarge is a configuration qualifier for extra large screens. When you append this string to a resource directory name (such as layout-xlarge), it indicates to the system that these resources are to be used on devices that have an extra large screen.

Table 1. Configuration qualifiers that allow you to provide special resources for different screen configurations.

Screen characteristic Qualifier Description
Size small Resources for small size screens.
normal Resources for normal size screens. (This is the baseline size.)
large Resources for large size screens.
xlarge Resources for extra large size screens.
Density ldpi Resources for low-density (ldpi) screens (~120dpi).
mdpi Resources for medium-density (mdpi) screens (~160dpi). (This is the baseline density.)
hdpi Resources for high-density (hdpi) screens (~240dpi).
xhdpi Resources for extra high-density (xhdpi) screens (~320dpi).
nodpi Resources for all densities. These are density-independent resources. The system does not scale resources tagged with this qualifier, regardless of the current screen’s density.
tvdpi Resources for screens somewhere between mdpi and hdpi; approximately 213dpi. This is not considered a “primary” density group. It is mostly intended for televisions and most apps shouldn’t need it—providing mdpi and hdpi resources is sufficient for most apps and the system will scale them as appropriate. If you find it necessary to provide tvdpi resources, you should size them at a factor of 1.33*mdpi. For example, a 100px x 100px image for mdpi screens should be 133px x 133px for tvdpi.
Orientation land Resources for screens in the landscape orientation (wide aspect ratio).
port Resources for screens in the portrait orientation (tall aspect ratio).
Aspect ratio long Resources for screens that have a significantly taller or wider aspect ratio (when in portrait or landscape orientation, respectively) than the baseline screen configuration.
notlong Resources for use screens that have an aspect ratio that is similar to the baseline screen configuration.

We will start with an example.

Please take a look at the screenshot which describes the above description.

L-2

The main.xml shown inside each folder may be same or different.
Android will automatically select each layout according to the Device resolution whether it is small, normal or HD.

i.e if the Device is of normal resolution and in the portrait mode then the layout inside “layout-normal” will be selected. When we rotate the same device for landscape then then layout under “layout-normal-land” will be selected.

L-1

L-2

Please leave your valuable comments on this post.

2 thoughts on “How to set different layouts for Portrait and Landscape in Android?

  1. Pingback: How to start with ActionBar in Android?

Leave a Reply

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