0

I am developing an android application which needs to have different layouts for tablet in landscape and other screen sizes. I've created different layouts in different folders like it is suggested in android tutorial (http://developer.android.com/training/multiscreen/screensizes.html).

So I have layout-xlarge, layout-xlarge-land and so on and layout preview seems to recognize those layouts for different devices however when I launch my application, the layouts are no longer recognized as per device. Following code always returns normal size layout:

int screenLayout = context.getResources().getConfiguration().screenLayout;
        screenLayout &= Configuration.SCREENLAYOUT_SIZE_MASK;

switch (screenLayout) {
       case Configuration.SCREENLAYOUT_SIZE_XLARGE:
          initXLargeLayout();
          return;
       default:
          initViePagerDefaultLayout();
          return;
    }

I could not find anyone with similar problem. Should I add something to manifest file?

harshal
  • 592
  • 6
  • 25

2 Answers2

0

I think, instead of using your int screenLayout you should do something like that: https://stackoverflow.com/a/9308284/4907293

Community
  • 1
  • 1
Sebastian
  • 318
  • 2
  • 12
0

That might be because you're launching on a device that does not have an xlarge screen, so it goes into your default. What device are you using?

By the way, you don't have to do that switch-case code because Android detects the configuration for you and selects from your layout folders (xlarge, mdpi, etc) and uses the appropriate layout automatically.

Your folder structure should look like this:

res
├── layout
│   └── activity_main.xml
├── layout-xlarge
|   └── activity_main.xml
└── layout-xlarge-land
    └── activity_main.xml

You just have to do setContentView(R.layout.activity_main.xml) once in your code and Android will take care of choosing which of those to use, depending on the device.

I suggest find out what configuration your device is, maybe it's just a large or some other size.

bytehala
  • 665
  • 1
  • 10
  • 25
  • I am using Nexus 10.1 and in preview it is recognized as xlarge device. The only reason why I've done switch statement is because in one mode I have viewpager and I have to set adapter and so on. I tried Sebastian's method but it does not work either. I'm pretty sure I had to do some minor mistake but I cant figure out which one. – Tobiasz Dziubinski May 17 '15 at 10:50
  • aww sorry to hear that, how about http://stackoverflow.com/questions/14556800/layout-folders-for-google-nexus-7-and-10 – bytehala May 17 '15 at 10:55
  • I already tried layout-xhdpi, layout-sw720dp, layout-xlarge-xhdpi but it always loads normal... – Tobiasz Dziubinski May 17 '15 at 11:11
  • After I deleted everything beside default and layout-xhdpi is works but I do not really want to recognize layout by dpi but by size of the display... – Tobiasz Dziubinski May 17 '15 at 11:35
  • Have you tried logging the resulting screenLayout and comparing it to this http://developer.android.com/reference/android/content/res/Configuration.html#SCREENLAYOUT_SIZE_XLARGE – bytehala May 17 '15 at 11:44
  • Sorry for such a delay. It is my homework so as soon as I found the walk around solution I dropped this problem however I will come back to it and post a solution. It appears that my program recognizes some other tablet but not Nexus 10. Strange issue but certainly worth investigating. – Tobiasz Dziubinski May 22 '15 at 12:19