How to detect tablet device

3 votes · 3 comments

How is the best way to programmatically detect whether the end-user is using a smartphone or a (honeycomb) tablet?

From my blog post: How to detect tablet device http://www.jjoe64.com/2011/08/how-to-detect-tablet-device.html

raw ·
copy
· download
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; }
Add a comment

3 Comments

ups there's a bug. Unfortunately I can't edit the post. Here's the correct code snipped:

    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;
}
Reply · Aug. 3, 2011, 8:51 a.m.

Actually, if you're testing for the SDK version, you don't have to use reflection at all! That's just making the code less readable, and making the app use reflection for no reason.

If you're inside that if block, then you can just run the method directly!

Reply · Nov. 15, 2011, 11:39 p.m.

AVCHD Video Converter enables Windows user to convert and import AVCHD, AVCHD lite, MTS, M2TS,TRP, TP, TS files to Windows Movie Maker WMV, AVI, MPG, iPad 2, iPod, iPhone 4S, Kindle Fire, Samsung Galaxy Tab 10.1 etc.

Reply · Feb. 21, 2012, 4:57 a.m.