Screen density is a ratio of resolution and display size, which can be quantified as dots per inch, or dpi. The higher the dpi, the smaller each individual pixel is, and the greater clarity.
Pixels is the most granular unit of measure. Pixel density refers to how many pixels have been squeezed into a physical amount of space (often an inch). Something like 1920x1080, 2560x1440, etc.
DPI is dots per inch unit and helps to measure the size of a pixel. It is an indicative of how many pixels can be found in one inch and is calculated by dividing the number of pixels across or down a screen by the width or height in inches. A higher DPI means each individual pixel must be smaller and smaller in order to be able to fit into the available space, meaning the clearer the screen will be and the higher the level of detail that screen can draw. Simply put, a higher DPI means more detail is displayed per inch, but does not necessarily correlate with a higher screen resolution.
DP is the density-independent pixel unit. One DP is one pixel on a 160 DPI screen. Independent pixel because the unit of measure is independent of the density, it is independent of the screen dimension and resolution. Whether the density is high or low, the elements on the screen will have approximatively the same size/look. The density-independent pixel is equivalent to one physical pixel on a 160 DPI screen, the baseline density assumed by the platform (as described later in this document). At run time, the platform transparently handles any scaling of the DP units needed, based on the actual density of the screen in use.
The formulas for calculation are following
px = dp * (dpi/160)
dp = (px * 160) / dpi
or use converter.
For example, on 240 DPI screen, 1 DP would equal 1.5 physical pixels. Using DP units to define your application’s UI is highly recommended, as a way of ensuring proper display of your UI on different screens.
In Android, user interfaces can be created in xml, and programmatically in code. In order to express a form of distance or length, there are several units for dimensions. These can be used on elements to set widths, heights, margins, padding, and more.
Let's look at code for getting Screen Density and Screen DensityDPI, width and height of the android device using DisplayMetrics
.
DisplayMetrics
is a structure describing general information about a display, such as its size, density, and font scaling.
public class DeviceDimensionsHelper { // DeviceDimensionsHelper.getDisplayWidth(context) => (display width in pixels) public static int getDisplayWidth(Context context) { DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); return displayMetrics.widthPixels; } // DeviceDimensionsHelper.getDisplayHeight(context) => (display height in pixels) public static int getDisplayHeight(Context context) { DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); return displayMetrics.heightPixels; } // DeviceDimensionsHelper.convertDpToPixel(25f, context) => (25dp converted to pixels) public static float convertDpToPixel(float dp, Context context){ Resources r = context.getResources(); return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()); } // DeviceDimensionsHelper.convertPixelsToDp(25f, context) => (25px converted to dp) public static float convertPixelsToDp(float px, Context context){ Resources r = context.getResources(); DisplayMetrics metrics = r.getDisplayMetrics(); float dp = px / (metrics.densityDpi / 160f); return dp; }