6

Why did I have this Alert message in my xml layouts in android

the message is Avoid using "px" as unite, use "dp" instead

RiadSaadi
  • 391
  • 2
  • 7
  • 16
  • Because there are thousands of different screen resolutions and you must use dp (deep Point) that is calculated in base of screen density – Dimentar Dec 06 '13 at 16:34
  • 1
    @Dimentar DP is not "deep point". `D`evice independent `P`ixels. – Simon Dec 06 '13 at 16:56
  • @Dimentar You can call it whatever you like, but please don't teach people your made-up acronyms. It only serves to confuse them and the people they work with. – Cat Dec 06 '13 at 17:40
  • The question was Not "That mean DP?" BUT "Why avoid using PX?". My mistake with Deep Point, but this mistake doesn't mean that my answer is wrong. – Dimentar Dec 06 '13 at 23:45
  • dp is a unit of length equal to 1/160 inches and living proof that Americans will use anything but the metric system. By default, there should be a unit that let's you define dimensions as a percentage of the screen's width and/or height. While dp could be useful if you want something to be a certain size relative to the user's fingers, it's much more important to make sure everything fits on screen, which is why android should seriously consider making percentages a unit alongside pixels and dps. – Math Machine Sep 30 '22 at 18:26

4 Answers4

2

It's a best practices thing.

dp = Density-independent pixel

A pixel is not a pixel is not a pixel. Some screens have 72 pixels per inch some have 200+

Read more here: http://developer.android.com/guide/practices/screens_support.html

Lenny
  • 5,663
  • 2
  • 19
  • 27
2

DP should always be used as the layout will better scale for devices that have lower/higher DPIs. If you use px for the layout its a fixed will use this many pixels, therefore the layout component could either be really small or massive and not look good.

dp should always be used as it handles the scaling for various devices much better.

Check http://developer.android.com/guide/practices/screens_support.html for more info.

Boardy
  • 35,417
  • 104
  • 256
  • 447
2

The answer is right there: What is the difference between "px", "dp", "dip" and "sp" on Android?

pixels is the exact number, and dip is dynamic: better to keep the view you want on any type of screen

Community
  • 1
  • 1
Charline
  • 283
  • 1
  • 2
  • 13