How to detect a tablet device in Android?

前端 未结 4 863
无人共我
无人共我 2021-01-03 07:38

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?

相关标签:
4条回答
  • 2021-01-03 07:57

    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

    0 讨论(0)
  • 2021-01-03 08:06

    Thinking on the "new" acepted directories (values-sw600dp for example) 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 on this list you can find some of the DP of 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

    0 讨论(0)
  • 2021-01-03 08:18

    I would introduce "Tablet mode" in application settings which would be enabled by default if resolution (use total pixel threshold) suggests it.

    IFAIK Android 3.0 introduces real tablet support, all previous versions are intended for phones and tablets are just bigger phones - got one ;)

    0 讨论(0)
  • 2021-01-03 08:22

    place this method in onResume() and 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;
    
        }
    

    generally tablets starts after 6 inch size.

    0 讨论(0)
提交回复
热议问题