Search code examples
androidandroid-layoutdpi

Android baseline dpi does not help calculate screen percentage


If a layout has android:layout_width="160dp" it renders half the screen on the hdpi Nexus S emulator.

I was expecting the Nexus 4 emulator (xhdpi) and the Nexus 5 emulator (xxhdpi) to scale accordingly, and show me a layout that's taking up half the screen, but they did not. They were a bit less than half (2/5 ish).

What am I missing? I thought that the XML environment always uses a specific screen, so I can base my calculations from there.

If 320dp = 480px / (240dpi / 160) and the Nexus S is hdpi (high) ~240dpi. Makes sense that it's 160dp is half the screen.

Then, the Nexus 5 is xxhdpi (extra-extra-high) ~480dpi with an HD resolution.

So, 1080px / (480dpi / 160) = 360dp!

What? Why?!

Should I avoid using dp when I want to divide my screen assets? Rely on weight and grid?


Solution

  • Indeed, as Budius commented, you cannot rely on dp to divide your screen into segments, since it only helps create a relative understanding of the size so that it reduces the amounts of calculated re-sizing you need to do, but does not effectively replace all unit sizes, such as what a raw percentage would do.

    Using weights is the way to go.