TheLettuceMaster
TheLettuceMaster

Reputation: 15734

How to check Tablet Size Programatically in Android

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?

Upvotes: 0

Views: 1391

Answers (2)

MH.
MH.

Reputation: 45493

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.

Upvotes: 2

Kevin Coppock
Kevin Coppock

Reputation: 134674

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.

Upvotes: 3

Related Questions