Friday, February 10, 2012

How to detect whether device is tablet or android smartphone?



My first idea was just to check the API level, but in future the tablets and smartphones could use the same Android version.

The next challenge is to make the code backward-compatible with reflection, because the low level Android devices API level < 9 doesn't support the required method for checking the screen resolution.

Now here's the code to check for a tablet device:
private boolean isTabletDevice() {
if (android.os.Build.VERSION.SDK_INT >= 11) { // honeycomb
// test screen size, use reflection because isLayoutSizeAtLeast is only available since 11
Configuration con = getResources().getConfiguration();
try {
Method mIsLayoutSizeAtLeast = con.getClass().getMethod("isLayoutSizeAtLeast");
Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con, 0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE
return r;
} catch (Exception x) {
return false;
}
}
return false;
}

No comments:

Post a Comment