How to get screen width ??
DisplayMetrics metrics = getResources().getDisplayMetrics(); int f = metrics.densityDpi; // getWindowManager().getDefaultDisplay().getMetrics(metrics); int width = metrics.widthPixels; int dp = (int)(width / (metrics.density));
I do this, so let's assume that my density is 120..and the pixel width is 240 .. through the calculation, I get 240 / 0.75 .... so do I have 320 widthdpi?
However, this is not the case .... the widthdpi is less than 320, I think ... because 320 goes wrong with me on the screen .... using 120 works.
on Nexus 4 ... with a width of 768 and a density of dpi 320 ... I divide 768/2 and get 384 of the correct density dpi (works)
But on others, such as 240, 160, 120 dpi ... calculating the dpi width seems wrong ...
source share