If I have android device with 5 inches diagonal screen size and 2.3 inches screen width and its screen supports 1080*1920 pixels what is the nearest type of its screen density? How can solve this question?
2 Answers
You should use sp
for font sizes and dp
for anything else. See this SO post regarding screen density, terminology, and proper uses: Difference between px, dp, dip and sp in Android?
Also, here is a link to the android developer's API guide for supporting multiple screens in your applications: http://developer.android.com/guide/practices/screens_support.html

- 5,308
- 3
- 32
- 61
You can find out device display info reading DisplayMetrics from Activity
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int dpi = dm.densityDpi;
dm.densityDpi
will give you device dpi bucket expressed in dots-per-inch
You can also get other display metrics like:
float density - The logical density of the display - scaling factor
float scaledDensity - A scaling factor for fonts displayed on the display.
int widthPixels - The absolute width of the display in pixels.
int heightPixels - The absolute height of the display in pixels.
float xdpi - Physical pixels per inch of the screen in the X dimension.
float ydpi - Physical pixels per inch of the screen in the Y dimension.
However, for some devices xdpi
and ydpi
metrics are returning wrong numbers and you cannot rely on them. Since API 17 you can also use getRealMetrics()
. It should give you accurate values for xdpi
and ydpi
.
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(dm);

- 27,212
- 44
- 82
- 159