1

I'm testing on a Samsung Galaxy Note 3 which according to the docs is 1080 x 1920 pixels (~386 ppi pixel density). I want to figure out what the screen width in dp is so I can properly apply and check the new size qualifiers.

I've measured the screen width (in portrait) with a good 'ole ruler = 71mm = 2.8 inches 1080 / 2.8 = 386 ppi. Great, this matches the stated density.

I'm trying to figure out how many dp units the screen is wide.

dp = px / (dpi / 160) from here
= 1080 / (386 / 160) = 1080 / 2.4125 = 447.7 dp

So if I do this:

<TextView
android:id="@+id/density_test"
android:layout_width="447dp"
android:layout_height="wrap_content"
android:background="#ffff0000"
android:text="DENSITY TEST 447dp" />

then on my Galaxy Note 3 this view should be almost exactly the width of the screen. But it isn't, it's way wider.

So I set the above view to layout_width="300dp", measured how wide it was with my ruler and extrapolated the actual screen width in dp, based on this test:

300dp = 59mm

Total screen width is 71mm

so extrapolating screen width in dp = (71 / 59) * 300 = 361dp

361dp != 447.7dp

What is going on here? I feel like I must be doing something dumb.

Edit: I tried putting the layout file inside /res/layout-xhdpi but it had no effect. I thought maybe it was scaling it by xhdpi / mdpi since I had the xml in the default /res/layout dir.

enter image description here

sleep
  • 4,855
  • 5
  • 34
  • 51
  • 1
    [Related measurements](http://stackoverflow.com/questions/21265670/samsung-note-2-and-note-3-screen-bucket). – stkent Oct 30 '14 at 02:09

1 Answers1

4

The actual pixel size is calculated based on dp, but not as accurate as the formula dp = px / (dpi / 160) does. Indeed, it first determines the screen density (ldpp, mdph, hdpi, xhdpi, xxhdpi, xxxhdpi) and using that density to get one ratio from {0.75, 1.0, 1.5, 2.0, 3.0, (4.0)}.

In you case, the system deems 386dpi as xxhdpi, so it calculates the value by multiplying 3.0, and 360dp would exactly fill the width of your screen (1080p)

Qianqian
  • 2,104
  • 18
  • 28