Anders Nysom
Anders Nysom

Reputation: 101

Android: which UI element size unit to use (if dp not working)

Im developing an App which is going to be used on Samsung Galaxy SII and Samsung Galaxy tab 7.7 ONLY. The sizes of all the UI elements have been specified in dp's as recommended by the Android developer guide. As I understood it, this would ensure that elements maintain the same perceived size to the user across different screens. My problem though is that all UI elements appear much smaller on the 7.7 inch device (around 2/3 of what they look like on the SII).

I would like them to be of equal size on the tablet, or perhaps even bigger. I have tested the different unit formats for measuring size, and when i specify them in inch or mm they have the same size, and when in px they are bigger on the tablet..

But is it really the correct approach to use any of those 3 units, and if so, then why do Android recommend dp's??

I hope anyone out there can clarify this :)

Upvotes: 3

Views: 1486

Answers (2)

nibarius
nibarius

Reputation: 4117

As I understood it, this would ensure that elements maintain the same perceived size to the user across different screens.

Using dp ensures similar perceived size across different screens, but not identical perceived size as you have noticed.

The dp unit is based on the densityDpi of the device which is set by the device to a density bucket with similar dpi as the true physical dpi of the device. When the two values are different anything dp dependent will get a slightly different size than expected.

Usually this doesn't give that big differences across devices, but you have found a really bad combination where each device differs quite a lot in the opposite directions making this difference very visible.

My problem though is that all UI elements appear much smaller on the 7.7 inch device (around 2/3 of what they look like on the SII).

Samsung Galaxy S2 has a physical dpi of 218 and reports 240 as densityDpi, that is all dp measures are 10% larger than on a device where physical dpi exactly matches densityDpi.

Samsung Galaxy Tab 7.7 has a physical dpi of 197 and reports 160 as densityDpi, so here all dp measures are 81% of what it would be on a device where physical dpi exactly matches densityDpi.

As you can see these two devices differs from their ideal dpi values and in different directions. So if you compare these two devices with each other everything drawn using dp will look like its about 35% bigger on Galaxy S2 compared to Galaxy Tab 7.7

I would like them to be of equal size on the tablet, or perhaps even bigger. I have tested the different unit formats for measuring size, and when i specify them in inch or mm they have the same size, and when in px they are bigger on the tablet..

You should avoid using pixels since this doesn't take the dpi of the device into account, so as you notice everything will be much bigger on the galaxy tab 7.7 since it has lower dpi than the galaxy s2.

The mm and inch units depends on the physical dpi of the screen so they will look the same across devices so these are probably the units you want to use.

There are some devices where these units can't be used reliably, but since you are developing an app that will only be used on known devices that doesn't have this problem it should be safe for you to use them. For more details about this, please see my answer to the question Why Lint shows warning when using in (inch) or mm (millimeter) units as dimension?

But is it really the correct approach to use any of those 3 units, and if so, then why do Android recommend dp's??

It is correct to use both mm/inch and dp, but they work slightly different so you have to consider what is important for you when you choose which unit you should use.

If something should be approximately x inch/mm, but it doesn't matter if it's a bit off it may be better to use the dp unit. Since the dp unit is tied to the density bucket the device belongs it is possible to know how many pixels x dp maps to for each density bucket. This means that it's possible to create graphics tailored for each density bucket which then can be used by the application without having to do any scaling of the graphics.

As I mentioned earlier, if you need something to be x inch/mm on the screen you should use the inch/mm unit. But doing this will cause graphics to be scaled to the wanted size so things might be a bit slower and not look quite as good due to the scaling.

Upvotes: 2

Lucas Arrefelt
Lucas Arrefelt

Reputation: 3929

Dp works to use in most cases but if I were you i would specify different layout folders, in this case a layout-xlarge/ for your 7.7 inch device and a layout-normal/ for your Galaxy SII.

Then duplicate your layout xml files and put in these folders, and specify position/sizes according to the device that will use it.

enter image description here

Information from here: http://developer.android.com/guide/practices/screens_support.html#support

Upvotes: 1

Related Questions