How to get the screen density programmatically in android?
I mean: How to find the screen dpi of the current device?
The following answer is a small improvement based upon qwertzguy's answer.
double density = getResources().getDisplayMetrics().density;
if (density >= 4.0) {
//"xxxhdpi";
}
else if (density >= 3.0 && density < 4.0) {
//xxhdpi
}
else if (density >= 2.0) {
//xhdpi
}
else if (density >= 1.5 && density < 2.0) {
//hdpi
}
else if (density >= 1.0 && density < 1.5) {
//mdpi
}
Try this:
DisplayMetrics dm = context.getResources().getDisplayMetrics();
int densityDpi = dm.densityDpi;
You Should Try This. Just Added a Method which will find and Show the Toast. That in Which Category the Device Falls.
public static int differentDensityAndScreenSize(Context context) {
int value = 20;
String str = "";
if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
switch (context.getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
str = "small-ldpi";
value = 20;
break;
case DisplayMetrics.DENSITY_MEDIUM:
str = "small-mdpi";
value = 20;
break;
case DisplayMetrics.DENSITY_HIGH:
str = "small-hdpi";
value = 20;
break;
case DisplayMetrics.DENSITY_XHIGH:
str = "small-xhdpi";
value = 20;
break;
case DisplayMetrics.DENSITY_XXHIGH:
str = "small-xxhdpi";
value = 20;
break;
case DisplayMetrics.DENSITY_XXXHIGH:
str = "small-xxxhdpi";
value = 20;
break;
case DisplayMetrics.DENSITY_TV:
str = "small-tvdpi";
value = 20;
break;
default:
str = "small-unknown";
value = 20;
break;
}
} else if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
switch (context.getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
str = "normal-ldpi";
value = 82;
break;
case DisplayMetrics.DENSITY_MEDIUM:
str = "normal-mdpi";
value = 82;
break;
case DisplayMetrics.DENSITY_HIGH:
str = "normal-hdpi";
value = 82;
break;
case DisplayMetrics.DENSITY_XHIGH:
str = "normal-xhdpi";
value = 90;
break;
case DisplayMetrics.DENSITY_XXHIGH:
str = "normal-xxhdpi";
value = 96;
break;
case DisplayMetrics.DENSITY_XXXHIGH:
str = "normal-xxxhdpi";
value = 96;
break;
case DisplayMetrics.DENSITY_TV:
str = "normal-tvdpi";
value = 96;
break;
default:
str = "normal-unknown";
value = 82;
break;
}
} else if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
switch (context.getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
str = "large-ldpi";
value = 78;
break;
case DisplayMetrics.DENSITY_MEDIUM:
str = "large-mdpi";
value = 78;
break;
case DisplayMetrics.DENSITY_HIGH:
str = "large-hdpi";
value = 78;
break;
case DisplayMetrics.DENSITY_XHIGH:
str = "large-xhdpi";
value = 125;
break;
case DisplayMetrics.DENSITY_XXHIGH:
str = "large-xxhdpi";
value = 125;
break;
case DisplayMetrics.DENSITY_XXXHIGH:
str = "large-xxxhdpi";
value = 125;
break;
case DisplayMetrics.DENSITY_TV:
str = "large-tvdpi";
value = 125;
break;
default:
str = "large-unknown";
value = 78;
break;
}
} else if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
switch (context.getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
str = "xlarge-ldpi";
value = 125;
break;
case DisplayMetrics.DENSITY_MEDIUM:
str = "xlarge-mdpi";
value = 125;
break;
case DisplayMetrics.DENSITY_HIGH:
str = "xlarge-hdpi";
value = 125;
break;
case DisplayMetrics.DENSITY_XHIGH:
str = "xlarge-xhdpi";
value = 125;
break;
case DisplayMetrics.DENSITY_XXHIGH:
str = "xlarge-xxhdpi";
value = 125;
break;
case DisplayMetrics.DENSITY_XXXHIGH:
str = "xlarge-xxxhdpi";
value = 125;
break;
case DisplayMetrics.DENSITY_TV:
str = "xlarge-tvdpi";
value = 125;
break;
default:
str = "xlarge-unknown";
value = 125;
break;
}
}
// The Toast will show the Device falls in Which Categories.
Toast.makeText(MainActivity.this, ""+str, Toast.LENGTH_SHORT).show();
return value;
}
http://www.androidwarriors.com/2016/01/how-to-find-different-devices-screen.html
This should work.
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels; //320
int height = dm.heightPixels; //480
Another way to get the density loaded by the device:
Create values
folders for each density
Add a string resource in their respective strings.xml
:
<string name="screen_density">MDPI</string> <!-- ..\res\values\strings.xml -->
<string name="screen_density">HDPI</string> <!-- ..\res\values-hdpi\strings.xml -->
<string name="screen_density">XHDPI</string> <!-- ..\res\values-xhdpi\strings.xml -->
<string name="screen_density">XXHDPI</string> <!-- ..\res\values-xxhdpi\strings.xml -->
<string name="screen_density">XXXHDPI</string> <!-- ..\res\values-xxxhdpi\strings.xml -->
Then simply get the string resource, and you have your density:
String screenDensity = getResources().getString(R.string.screen_density);
If the density is larger than XXXHDPI
, it will default to XXXHDPI
or if it is lower than HDPI
it will default to MDPI
I left out LDPI
, because for my use case it isn't necessary.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi) {
case DisplayMetrics.DENSITY_LOW:
break;
case DisplayMetrics.DENSITY_MEDIUM:
break;
case DisplayMetrics.DENSITY_HIGH:
break;
}
This will work on API level 4 and higher.