Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect a tablet device in Android?

I am trying to port my application developed for smartphones to the tablets with minor modifications. Is there an API in Android to detect if the device is tablet?

I can do it by comparing the screen sizes, but what is the correct approach to detect a tablet?

like image 778
Shafi Avatar asked Dec 01 '22 07:12

Shafi


1 Answers

I don't think there are any specific flags in the API.

Based on the GDD 2011 sample application I will be using these helper methods:

public static boolean isHoneycomb() {
    // Can use static final constants like HONEYCOMB, declared in later versions
    // of the OS since they are inlined at compile time. This is guaranteed behavior.
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
}

public static boolean isTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

public static boolean isHoneycombTablet(Context context) {
    return isHoneycomb() && isTablet(context);
}

Source

like image 91
roflharrison Avatar answered Dec 15 '22 01:12

roflharrison