How to find the device as LDPI MDPI HDPI or XHDPI [duplicate]

你说的曾经没有我的故事 提交于 2019-11-28 05:54:05
Pragnani

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

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int density = dm.densityDpi;

The density variable is a constant defined in DisplayMetrics corresponding to the different dpis.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!