77

I'm not sure to have understood well the role played by the several Android /res/layout folders.

layout: general layouts

layout-port: layout for widget that must change for portrait orientation

layout-land: layout for widget that must change for landscape orientation

For example in the Home sample application, under layout-port and layout-land folders there is only one home.xml file.

Have I understood right?

Giorgio Vespucci
  • 1,586
  • 3
  • 18
  • 30

3 Answers3

117

If you are in landscape or portrait Android looks for the layout file in either the -port or -land directory first, if it's not found then it falls back to the default layout directory.

You can read about providing alternative resources here. There are many more options than just land and port.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
  • 1
    In this case, the other layouts do not change depending on orientation so they are just in the layout folder, whereas home is specific to portrait or landscape – Cameron Mar 23 '11 at 15:47
  • I'm surprised there isn't some sort of orientation specifier for each property, to indicate different values for each. I understand that in the case of resolutions that could get messy, but since there's only two possible orientations (and always will be), it might be nice to not have to maintain two separate files for a layout if, for example, only one property needs to differ between orientations. – RTF Jan 05 '14 at 02:31
14

If you put home.xml in layout-port folder, when your device is in portrait orientation it will use the file: layout-port/home.xml.

If you put home.xml in layout-land folder, when your device in landscape orientation it will use the file: layout-land/home.xml.

Meaning for different orientation modes like portrait and landscape... we use two home.xml files; one in layout-port and the other in layout-land. If on the other hand you want to use the same layout file for both, you can just put home.xml in the layout folder and remove it from layout-land and layout-port.

Ali
  • 12,354
  • 9
  • 54
  • 83
Uday
  • 5,933
  • 9
  • 44
  • 76
  • 1
    I've made seprate layout xml files for both layout and layout-land. But when I change the orientation to landscape, its layout-land file is not getting displayed by the android. What is the problem? – YuDroid Jul 09 '12 at 05:58
  • 10
    if you have two layout files in -port and -land and they don't switch when you change the device orientation, you probably specified android:configChanges="orientation" in the AndroidManifest.xml. In that case specify again setContentView() in the onConfigurationChanged() or remove that AndroidManifest.xml directive. – Emmanuel Touzery Jul 17 '12 at 08:37
3

Create folder in res layout-land or layout-port

layout-port: layout for widget that must change for portrait orientation

layout-land: layout for widget that must change for landscape orientation

Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103