How to find the device as LDPI MDPI HDPI or XHDPI

Sreeram picture Sreeram · Feb 25, 2013 · Viewed 31k times · Source

Without any code snippet for the application, how to get screen resolution and length of the screen. How could I find whether the device is ldpi, mdpi , hdpi or xhdpi ?

Answer

Pragnani picture Pragnani · Feb 25, 2013

Edit: use DisplayMetrics to get the density of the screen

getResources().getDisplayMetrics().densityDpi;

this will return the int value that represents the following constants. DisplayMetrics.DENSITY_LOW ,DisplayMetrics.DENSITY_MEDIUM, DisplayMetrics.DENSITY_HIGH, DisplayMetrics.DENSITY_XHIGH

  int density= getResources().getDisplayMetrics().densityDpi;

switch(density)
{
case DisplayMetrics.DENSITY_LOW:
   Toast.makeText(context, "LDPI", Toast.LENGTH_SHORT).show();
    break;
case DisplayMetrics.DENSITY_MEDIUM:
     Toast.makeText(context, "MDPI", Toast.LENGTH_SHORT).show();
    break;
case DisplayMetrics.DENSITY_HIGH:
    Toast.makeText(context, "HDPI", Toast.LENGTH_SHORT).show();
    break;
case DisplayMetrics.DENSITY_XHIGH:
     Toast.makeText(context, "XHDPI", Toast.LENGTH_SHORT).show();
    break;
}

This will return following constants based on thsi you can identify the device


Try this

int screenSize = getResources().getConfiguration().screenLayout &
        Configuration.SCREENLAYOUT_SIZE_MASK;

switch(screenSize) {
    case Configuration.SCREENLAYOUT_SIZE_LARGE:
        Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show();
        break;
    case Configuration.SCREENLAYOUT_SIZE_NORMAL:
        Toast.makeText(this, "Normal screen",Toast.LENGTH_LONG).show();
        break;
    case Configuration.SCREENLAYOUT_SIZE_SMALL:
        Toast.makeText(this, "Small screen",Toast.LENGTH_LONG).show();
        break;
    default:
        Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show();
}

Source Identifying screen resolutions