Friday, February 24, 2012

Android Code Snippet - Check if tablet or smartphone

Here is how to check if the user is using a smartphone or a tablet...

  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", int.class);
            Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con, 0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE
            return r;
        } catch (Exception x) {
            x.printStackTrace();
            return false;
        }
    }
    return false;
}

No comments:

Post a Comment