Tutorial by Examples

To retreive the screens width and height in pixels, we can make use of the WindowManagers display metrics. // Get display metrics DisplayMetrics metrics = new DisplayMetrics(); context.getWindowManager().getDefaultDisplay().getMetrics(metrics); These DisplayMetrics hold a series of informatio...
To get the screens density, we also can make use of the Windowmanagers DisplayMetrics. This is a quick example: // Get density in dpi DisplayMetrics metrics = new DisplayMetrics(); context.getWindowManager().getDefaultDisplay().getMetrics(metrics); int densityInDpi = metrics.densityDpi;
DP to Pixel: private int dpToPx(int dp) { return (int) (dp * Resources.getSystem().getDisplayMetrics().density); } Pixel to DP: private int pxToDp(int px) { return (int) (px / Resources.getSystem().getDisplayMetrics().density); }

Page 1 of 1