Tablet or Phone - Android

后端 未结 30 2534
难免孤独
难免孤独 2020-11-22 08:33

Is there a way to check if the user is using a tablet or a phone? I\'ve got problems with my tilt function and my new tablet (Transformer)

30条回答
  •  故里飘歌
    2020-11-22 09:10

    Thinking on the "new" acepted directories (values-sw600dp for example) i created this method based on the screen' width DP:

     public static final int TABLET_MIN_DP_WEIGHT = 450;
     protected static boolean isSmartphoneOrTablet(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;
    }
    

    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

提交回复
热议问题