I want to check this to fetch different images by internet. How to do that?
7 Answers
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
-
9Density values described at: http://developer.android.com/guide/practices/screens_support.html – esilver Jan 21 '14 at 21:52
-
1@SteD is it possible to tell what a device is going to be? eg consider the case where the dpi is 140 in the middle of the bucket range? does it round down or up? – wal Jun 29 '16 at 06:12
-
tvdpi is about 1.3 – Ethan_AI Oct 05 '16 at 21:40
-
4for nexus 6p i am getting 3.5 , which category will it fall into ? – Manohar Nov 16 '16 at 06:12
-
2oneplus3T has 2.625, what should we consider that in? – Parth Anjaria Nov 21 '17 at 09:38
-
13.5 Also. This doesn't definitively answer the question. – Keith Loughnane Mar 24 '20 at 13:06
-
Check my answer with interval approach. https://stackoverflow.com/a/64948456/5752473 – I.Step Nov 21 '20 at 21:59
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.

- 232,168
- 48
- 399
- 521
-
It's supposed to exist in SDK level 4 (platform 1.6) and up. What SDK level are you using? (In SDK 3, you can use `density` as suggested by SteD.) – Ted Hopp Feb 24 '11 at 02:49
-
I need to target platform1.5. The below answer maybe fit for my request. – virsir Feb 24 '11 at 02:51
-
4
-
FYI, with some new devices (like the Nexus 7), you should also include DENSITY_TV. – annie Nov 15 '12 at 18:13
-
1@annie - Good point. Thanks. As of API level 16, it should also include `DENSITY_XXHIGH`. I added a disclaimer to the answer to cover all future cases. :) – Ted Hopp Nov 15 '12 at 19:31
-
1looks like those values are in order. so you can use sequence of `if(dpi >= DisplayMetrics...)` if you don't want to miss specific dpi's. – M.kazem Akhgary Nov 26 '18 at 15:53
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

- 5,317
- 2
- 30
- 53
-
According to android documentation this will be also correct approach. This should be correct approach but should be refactored when new DENSITY_? values will be added in the api. Until than check my answer with density intervals: https://stackoverflow.com/a/64948456/5752473, also everyone correct me if I am wrong with interval approach. – I.Step Nov 21 '20 at 21:57
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));
}

- 6,627
- 2
- 58
- 83
You should always check the interval not only break point values!
According to android documentation: https://developer.android.com/reference/android/util/DisplayMetrics
Check it like this in Kotlin:
when {
val density: Float = context?.resources?.displayMetrics?.density
// ldpi
(density <= 0.75f) -> mulFactor = 0.75f
// mdpi
(density >= 0.75f && density <= 1.0f) -> mulFactor = 1.0f
// hdpi
(density > 1.0f && density <= 1.5f) -> mulFactor = 1.5f
// xhdpi
(density > 1.5f && density <= 2.0f) -> mulFactor = 2.0f
// xxhdpi
(density > 2.0f && density <= 3.0f) -> mulFactor = 3.0f
// xxxhdpi
(density > 3.0f) -> mulFactor = 4.0f
}
return mulFactor;

- 613
- 6
- 22
For React Native to check which size is currently of device
import { PixelRatio } from 'react-native';
switch(PixelRatio.get()) {
case 1:
return "mdpi";
case 1.5:
return "hdpi";
case 2:
return "xhdpi";
case 3:
return "xxhdpi";
case 3.5:
return "xxxhdpi";
}

- 600
- 7
- 10
-
This will not work. `PixelRatio.get()` will return a range of values depending on the display size set in Android settings, and the dpi. Those values will not co-incide with the exact values you've chalked out. – EnKrypt Aug 17 '19 at 00:34
On some devices (mine is Galaxy Tab3), both density and densityDpi return strange values like 1.33(density), 213(densityDpi). So my solution is to add these flag :
<item type="bool" name="is_mdpi">[bool]</item>
<item type="bool" name="is_hdpi">[bool]</item>
<item type="bool" name="is_xhdpi">[bool]</item>
<item type="bool" name="is_xxhdpi">[bool]</item>
to 4 values.xml files, put these under corresponding res/values-[xxx]/ folders.

- 555
- 7
- 11
-
6213dpi is a well-known density, "tvdpi": http://developer.android.com/guide/practices/screens_support.html#qualifiers – Christopher Orr Feb 04 '14 at 19:37