Search code examples
androidkindle-fireandroid-screen

How to detect 7" Android tablet in code


I am trying to detect 7" tablets in my code (i.e. Kindle Fire & Nook Color). However, simply testing for minimum dimensions 1024x600 is not good, because then the Galaxy Nexus would pass this test as well. Anybody has experience with detecting this kind of information?

Thanks, Igor

EDIT: I have found one way to detect Kindle Fire & Nook Color devices with the following code:

Activity activity = (Activity) activityContext;
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay().getHeight();
if ((width == 1024 && height == 600) || (width == 600 && height == 1024)) {    
        //Detects 7" tablets: i.e. Kindle Fire & Nook Color
        isTablet = true;
    }

Solution

  • To calculate the height and width of the device (in inches) you can use the following code.

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
    float widthInInches = metrics.widthPixels / metrics.xdpi;
    float heightInInches = metrics.heightPixels / metrics.ydpi;
    

    Then the size can be calculated

    double sizeInInches = Math.sqrt(Math.pow(widthInInches, 2) + Math.pow(heightInInches, 2));
    //0.5" buffer for 7" devices
    boolean is7inchTablet = sizeInInches >= 6.5 && sizeInInches <= 7.5; 
    

    As per Commonsware suggestion above, a potentially faster, but less obvious implementation.

    double sizeInInchesSquared = (widthInInches * widthInInches) + (heightInInches * heightInInches);
    //0.5" buffer for 7" devices (6.5^2 = 42.25) (7.5^2 = 56.25)
    boolean is7inchTablet = sizeInInchesSquared >= 42.25 && sizeInInchesSquared <= 56.25;