Tablet or Phone - Android

后端 未结 30 2140
难免孤独
难免孤独 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:20

    E.g. have one important difference (at least for my program) between the phone and tablet. It is the default orientation of the device. Phone has a portrait orientation, the tablet - landscape. And respectively method to determine the device:

    private static boolean isLandscapeDefault(Display display) {
        Log.d(TAG, "isTablet()");
        final int width = display.getWidth();
        final int height = display.getHeight();
    
        switch (display.getOrientation()) {
        case 0: case 2:
            if(width > height) return true;
            break;
        case 1: case 3:
            if(width < height) return true;
            break;
        }
        return false;
    }
    

    EDITED: Following the discussions with Dan Hulme changed the name of the method.

提交回复
热议问题