0

Right now, I do this:

    if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE) {
        actionBar.removeAllTabs();

    } else {  // other stuff to do 

    }

However, I want this to prove true ONLY if it is 10"+ tablets, or XLarge. Not 7 inch (e.g. Nexus 7). I want the 7" to be same layout as phone. Can I do this check pragmatically?

TheLettuceMaster
  • 15,594
  • 48
  • 153
  • 259

2 Answers2

3

You should avoid doing this in code, and instead make a resource-qualified folder for your layout defined for sw720dp (i.e. you should make your 10" layout under res/layout-sw720dp). Avoid using the large and xlarge qualifiers, and rely on the number of density-independent pixels available.

See this section on Supporting Multiple Screens for more info.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • The problem is, I have a `ViewPager` and Action Bar tabs that I can't control (remove) in XML without crashing the app. I don't need them on large screens, but I do on small. So if I did it that way as you say I get null pointer on the `ViewPager`. Now, can I do this programatically using the density-independant qualifiers? – TheLettuceMaster Dec 15 '12 at 01:36
  • 3
    There are a few different ways you could do that...one hackish yet very easy way to do this that I saw [here](http://stackoverflow.com/a/7284958/321697) would be to make a boolean in your values.xml, and another in `res/values-sw720dp/values.xml`, where it's false in the first, and true in the second. Then in code, get the value of the boolean. If it's true, you're on a sw720dp device. – Kevin Coppock Dec 15 '12 at 01:40
  • The second way would be to get the DisplayMetrics, get the pixel dimensions of the window area, divide it by (density / 160) (e.g. a 1920x1080 hdpi device would be (1080 / (240 / 160)) == (1080 / 1.5) == 720dp). – Kevin Coppock Dec 15 '12 at 01:42
  • Very nice. I like the hackish way. I will upvote your answer. Marked the other guy correct because that is what I am using at the moment. – TheLettuceMaster Dec 15 '12 at 01:48
2

Replace SCREENLAYOUT_SIZE_LARGE with SCREENLAYOUT_SIZE_XLARGE:

if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE) {
    /* implementation */
}

If possible avoid using the deprecated large and xlarge qualifiers and use the new sw<N>dp qualifier. However, depending on your target audience and platform, you may need to rely on the older approach, or potentially a mix of both.

MH.
  • 45,303
  • 10
  • 103
  • 116