Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check an Android device is HDPI screen or MDPI screen?

People also ask

What is Hdpi and Mdpi in Android?

mdpi. Resources for medium-density (mdpi) screens (~160dpi). (This is the baseline density.) hdpi. Resources for high-density (hdpi) screens (~240dpi).

How do I find the DPI of my Android phone?

To find the DPI, look at the Software Density entry under the Display section. For the Android version, look at the OS version under the Device section. This explicitly displays the version number.

What is Hdpi Mdpi Xhdpi?

Hdpi is a 1.5:1, and can be thought of as a HD (high-definition) display. And xhdpi is 2:1, much like Apple retina displays. . The normal mdpi is based on a 160 dpi screen, which again is the same as a single pixel unit in your graphics software.


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

// return 0.75 if it's LDPI
// return 1.0 if it's MDPI
// return 1.5 if it's HDPI
// return 2.0 if it's XHDPI
// return 3.0 if it's XXHDPI
// return 4.0 if it's XXXHDPI

You can check the screen density with:

switch (getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
    // ...
    break;
case DisplayMetrics.DENSITY_MEDIUM:
    // ...
    break;
case DisplayMetrics.DENSITY_HIGH:
    // ...
    break;
case DisplayMetrics.DENSITY_XHIGH:
    // ...
    break;
}

EDIT Be aware that as Android evolves, other values should be included in the switch cases. As of this edit, this includes DisplayMetrics.DENSITY_TV and DisplayMetrics.DENSITY_XXHIGH. Consult the docs for the latest info; I'm not going to bother maintaining this answer.


As of 2018, you can use the below method -

    public static String getDeviceDensityString(Context context) {
    switch (context.getResources().getDisplayMetrics().densityDpi) {
        case DisplayMetrics.DENSITY_LOW:
            return "ldpi";
        case DisplayMetrics.DENSITY_MEDIUM:
            return "mdpi";
        case DisplayMetrics.DENSITY_TV:
        case DisplayMetrics.DENSITY_HIGH:
            return "hdpi";
        case DisplayMetrics.DENSITY_260:
        case DisplayMetrics.DENSITY_280:
        case DisplayMetrics.DENSITY_300:
        case DisplayMetrics.DENSITY_XHIGH:
            return "xhdpi";
        case DisplayMetrics.DENSITY_340:
        case DisplayMetrics.DENSITY_360:
        case DisplayMetrics.DENSITY_400:
        case DisplayMetrics.DENSITY_420:
        case DisplayMetrics.DENSITY_440:
        case DisplayMetrics.DENSITY_XXHIGH:
            return "xxhdpi";
        case DisplayMetrics.DENSITY_560:
        case DisplayMetrics.DENSITY_XXXHIGH:
            return "xxxhdpi";
    }
}

But as @Ted pointed always consult the official docs before using


From the above answers, I combined them and created the below function:

    public static String getDeviceDensity(Context context){
    String deviceDensity = "";
    switch (context.getResources().getDisplayMetrics().densityDpi) {
        case DisplayMetrics.DENSITY_LOW:
            deviceDensity =  0.75 + " ldpi";
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            deviceDensity =  1.0 + " mdpi";
            break;
        case DisplayMetrics.DENSITY_HIGH:
            deviceDensity =  1.5 + " hdpi";
            break;
        case DisplayMetrics.DENSITY_XHIGH:
            deviceDensity =  2.0 + " xhdpi";
            break;
        case DisplayMetrics.DENSITY_XXHIGH:
            deviceDensity =  3.0 + " xxhdpi";
            break;
        case DisplayMetrics.DENSITY_XXXHIGH:
            deviceDensity =  4.0 + " xxxhdpi";
            break;
        default:
            deviceDensity = "Not found";
    }
    return deviceDensity;
}

Now, on which device you want to get the density information and which folder it will be used, just add the above method in that activity and add the below line in onCreate

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    Log.d("Screen Density: ", Helper.getDeviceDensity(this));

}