10

I have a layout that is different for portrait and landscape; however on tablets (e.g. really big screen, xlarge layout qualifier) I want to use only the portrait version for both orientations.

/res/layout/abc.xml

/res/layout-land/abc.xml

/res/layout-xlarge/abc.xml

Since the first and last layouts are identical, I was thinking of using an alias. But I can't figure out from the Android docs how to reference a qualifier specific layout in my statement...

Ideas?

Yossi
  • 1,226
  • 1
  • 16
  • 31
  • Why dont you load resources dynamically depending on resolution or aspect ratio or some condition by setting one default resource. – sat Jul 31 '11 at 19:38
  • 1
    I would rather let the platform take care of it automatically ... the less code the better... – Yossi Jul 31 '11 at 20:18
  • It gets more complicated. I am using these layouts for homescreen widgets, and apparently the merge / include technique doesn't work. – Yossi Aug 01 '11 at 02:06
  • 1
    posted the same question just now before I found yours... :/ no answers here means - there is no answer? *sigh* – Zordid Sep 14 '11 at 22:04
  • @Zordid's question: http://stackoverflow.com/q/7423413/253468 – TWiStErRob Jun 19 '16 at 09:19
  • I got issue with data binding – Mahdi Jul 28 '20 at 07:01

1 Answers1

9

I was fighting with exactly! same problem and finally found a solution, so even if question is quite old, maybe someone find it useful

  1. put portrait layout into /res/layout/abc.xml (you already have it)

  2. put landscape layout into /res/layout/abc_land.xml

  3. create file layout.xml with content

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <item name="abc" type="layout">@layout/abc_land</item>
    </resources>
    
  4. copy this file into your /res/values-land/ and /res/values-xlarge/ directories

Important is, that your layout.xml file contain reference to improved (landscape) file abc_land.xml, that is stored also in layout directory and also files are under values-x directories, not layout-x once.

Also I tried to put into layout directory two files abc_land.xml and abc_port.xml and create on them alias from /res/values-land/, /res/values-port/ and also /res/values-xlarge/ directories, and seems to work, so it's also usable solution to make in layouts some "order"!

Menion Asamm
  • 984
  • 11
  • 19
  • nono, I think correctly `layout.xml`. Check this [Android documentation](https://developer.android.com/training/multiscreen/screensizes.html#TaskUseAliasFilters). There is also description `And add these two files ...` where they use `layout.xml`. I expect that if you'll need to use more aliases, you just put it as a next ` – Menion Asamm Jan 01 '13 at 06:52