How to detect Android Tablet in Java?

Question:

Is there any way to detect that the app is running on a Tablet or Smartphone ?

So far I do it by detecting the width of pixeles , but I find the Samsung Galaxy S3, S4 range … which have more resolution than a tablet and is not optimal.

Answer:

Forget about detecting the pixel width or density, this method is the one I use and it is the one suggested by google:

public static boolean esTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

Additional info: http://developer.android.com/reference/android/content/res/Configuration.html#screenLayout

public void deterScreenSize() {

    if((getResources().getConfiguration().screenLayout &
        Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {   

        Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show();

    }else if ((getResources().getConfiguration().screenLayout & 
               Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {   

        Toast.makeText(this, "Normal sized screen" , Toast.LENGTH_LONG).show();

    }else if ((getResources().getConfiguration().screenLayout &
               Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {    

        Toast.makeText(this, "Small sized screen" , Toast.LENGTH_LONG).show();

    }else if ((getResources().getConfiguration().screenLayout & 
               Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {     

        Toast.makeText(this, "XLarge sized screen" , Toast.LENGTH_LONG).show();

    }else {
        Toast.makeText(this, "El tamaño de la pantalla no es ni X grande, ni grande, ni normal o pequeña" , Toast.LENGTH_LONG).show();
    }
}
Scroll to Top