Pixel density in android
You should use sp
for font sizes and dp
for anything else. See this SO post regarding screen density, terminology and correct usage: Difference between px, dp, dip and sp in Android?
Also, here is a link to the Android developer API guide for supporting multiple screens in your apps: http://developer.android.com/guide/practices/screens_support.html
source to share
You can get information about device readings DisplayMetrics from Activity
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int dpi = dm.densityDpi;
dm.densityDpi
will provide you with a pad for entry points 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
, ydpi
metrics also return incorrect numbers and you cannot rely on them. Since API 17, you can also use getRealMetrics()
. It should give you the exact values โโfor xdpi
and ydpi
.
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(dm);
source to share