How to detect a tablet device in Android?

I am trying to port an application designed for smartphones to tablets with minor changes. Is there an API in Android to determine if a device is a tablet?

I can do this by comparing screen sizes, but what is the right approach to detecting a tablet?

+4
source share
4 answers

I would introduce “Tablet Mode” in the application’s settings, which will be enabled by default if it allows (use a common pixel threshold).

IFAIK Android 3.0 represents real tablet support, all previous versions are for phones and tablets - it's just big phones - got one;)

+1
source

I do not think there are certain flags in the API.

Based on the sample GDD 2011 application, I will use 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

+18
source

Reflecting on the “new” intercepted directories (for example, values-sw600dp), I created this method based on the screen width "DP:

 /** * Returns true if the current device is a smartphone or a "tabletphone" * like Samsung Galaxy Note or false if not. * A Smartphone is "a device with less than TABLET_MIN_DP_WEIGHT" dpi * * @return true if the current device is a smartphone or false in other * case */ protected static boolean isSmartphone(Activity act){ DisplayMetrics metrics = new DisplayMetrics(); act.getWindowManager().getDefaultDisplay().getMetrics(metrics); int dpi = 0; if (metrics.widthPixels < metrics.heightPixels){ dpi = (int) (metrics.widthPixels / metrics.density); } else{ dpi = (int) (metrics.heightPixels / metrics.density); } if (dpi < TABLET_MIN_DP_WEIGHT) return true; else return false; } public static final int TABLET_MIN_DP_WEIGHT = 450; 

And in this list you will find some of the DP popular devices and tablet sizes:

Wdp / hdp

GALAXY Nexus: 360/567
XOOM: 1280/752
GALAXY NOTE: 400/615
NEXUS 7: 961/528
GALAXY TAB (> 7 & <10): 1280/752
GALAXY S3: 360/615

Wdp = Width dp
Hdp = Height dp

+1
source

put this method in onResume () and you can check.

 public double tabletSize() { double size = 0; try { // Compute screen size DisplayMetrics dm = context.getResources().getDisplayMetrics(); float screenWidth = dm.widthPixels / dm.xdpi; float screenHeight = dm.heightPixels / dm.ydpi; size = Math.sqrt(Math.pow(screenWidth, 2) + Math.pow(screenHeight, 2)); } catch(Throwable t) { } return size; } 

usually tablets start after a 6-inch size.

0
source

All Articles