Differentiate Android devices with code [8inch vs 10 inch tablet]

I am coding my app for two specific devices

Venue Dell 8 8inch

Samsung Galaxy Note 10.1 10inch

In my code, I want to write code differently for each of these devices for a small module, but I cannot tell the difference between the two

I used

double density = getResources().getDisplayMetrics().density;


but it returns the same for both +1.3312500715255737

through getDisplayMetrics()

I tried to get permission but it came back for both


so what am I using to differentiate between both of these devices in my code?


source to share

1 answer

You can use DisplayMetrics to get a whole bunch of information about the screen your application is running on.

First, we create a DisplayMetrics metric object:

DisplayMetrics displaymetrics = new DisplayMetrics();
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;


This will return the absolute width and height in pixels, so 1280x720 for Galaxy SIII, Galaxy Nexus, etc.

This usually does not help on its own, since when we work on Android devices we usually prefer to work with density independent pixels, dip.

float scaleFactor = displaymetrics .density;


From this result, we can calculate the number of density independent pixels for a specific height or width.

float widthDp = widthPixels / scaleFactor
float heightDp = heightPixels / scaleFactor


Using the information above, we know that if the smallest width of the device is more than 600 dp, the device is a 7 "tablet, if it is more than 720 dp, the device is a 10" tablet.

We can work out the smallest width using the min function of the Math class, passing in the height Dp and the width Dp to return the smallest width.

float smallestWidth = Math.min(widthDp, heightDp);

if (smallestWidth > 720) {
    //Device is a 10" tablet
else if (smallestWidth > 600) {
    //Device is a 7" tablet


However, this does not always give you an exact match, especially when dealing with obscure tablets that can distort their density like hdpi when it doesn't, or it might only be 800 x 480 pixels but still be 7 ".

In addition to these methods, if you ever need to know the exact dimensions of a device in inches, you can also fix it by using the metrics method for pixels per inch of screen.

float widthDpi = displaymetrics .xdpi;
float heightDpi = displaymetrics .ydpi;


You can use knowledge of how many pixels are in each inch of a device and the total number of pixels to determine how many inches the device is.

float widthInches = widthPixels / widthDpi;
float heightInches = heightPixels / heightDpi;


This will return the height and width of the device in inches. This is again not always useful for determining what type of device it is, since the declared size of the device is diagonal, all we have is height and width.

However, we also know that given the triangle's height and width, we can use the Pythagorean theorem to determine the length of the hypotenuse (in this case, the size of the screen diagonal).

//a² + b² = c²

//The size of the diagonal in inches is equal to the square root of the height in inches squared plus the width in inches squared.
double diagonalInches = Math.sqrt(
    (widthInches * widthInches) 
    + (heightInches * heightInches));


From this, you can determine if the device is a tablet or not:

if (diagonalInches >= 10) {
    //Device is a 10" tablet
else if (diagonalInches >= 7) {
    //Device is a 7" tablet




All Articles