How to detect a tablet device in Android?

前端 未结 4 895
无人共我
无人共我 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 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

提交回复
热议问题