Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get screen width in dpi?

How can I get the screen width dpi?

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 lets assume my densitydpi is 120..and widthpixel 240.. via calculation i get 240/0.75.... so I have 320 widthdpi?

However that's not the case....widthdpi is smaller then 320 I think...because 320 goes with me wrong in the screen.... using 120 works.

on Nexus 4...with width 768 and densitydpi 320...I divide 768/2 and i get 384 correct width density dpi (it works)

But on other ones like 240, 160, 120 density dpi..the calculation of width dpi seems wrong ...

like image 810
user3278732 Avatar asked Feb 25 '14 06:02

user3278732


People also ask

How do I know my screen width?

Use window. innerWidth and window. innerHeight to get the current screen size of a page.

How do I tell what my screen DPI is?

Go to Control Panel > Appearance and Personalization > Display. In the left(blue) column, click Set custom text size(DPI)

How do I find my screen width and height?

int height = displayMetrics. heightPixels; int width = displayMetrics.

How many PX is screen width?

In the case of a monitor with an industry-standard Full HD 1080p resolution, this display has a resolution of 1920 x 1080. This means that the screen will have a width of 1,920 pixels while the height of the screen will be 1,080 pixels.


1 Answers

Per the Supporting Multiple Screens guide's definition of DP

px = dp * (dpi / 160)

Therefore

dp = px / (dpi / 160)

or equivalently

dp = px * 160 / dpi

Remember that dp stands for 'density-independent pixel' - i.e., 1dp is the same physical size on a ldpi device as it is on an xxhdpi device. Therefore you should expect all phones to have roughly ~300-400dp of width, noting the bucket sizes by dp:

  • xlarge screens are at least 960dp x 720dp
  • large screens are at least 640dp x 480dp
  • normal screens are at least 470dp x 320dp
  • small screens are at least 426dp x 320dp
like image 126
ianhanniballake Avatar answered Sep 28 '22 08:09

ianhanniballake