2

I wand to handle the multiple screen handling in my app.

I see that in tutorials margin and padding are set in the dp.

I want to know if dp is the best unit for padding and margin? What other units are available for setting padding and margin?

vhu
  • 12,244
  • 11
  • 38
  • 48
Sohaib Raza
  • 41
  • 2
  • 10
  • It all depends on how your UI should look like on different screen sizes. – dsharew Oct 13 '15 at 06:22
  • The different units available are px and dp/dip for the padding and margin...yes dp is better than px in terms of handling different screen size ...for indepth information refer : http://stackoverflow.com/questions/2025282/difference-between-px-dp-dip-and-sp-in-android – Shadow Droid Oct 13 '15 at 06:23
  • I make the relative layout and i use the dp for margin and padding , if i run the app on different screen size, is the padding and margin automatically adjust when i am using dp? – Sohaib Raza Oct 13 '15 at 06:25
  • @SohaibRaza yes it will be adjusted based on screen size and density you can refer dochttp://developer.android.com/guide/practices/screens_support.html#density-independence – Shadow Droid Oct 13 '15 at 06:47

2 Answers2

2

In my opinion, using dp is the best choice for margin and padding.

The units available are:

dp Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".

sp Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.

mm Millimeters - based on the physical size of the screen.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Rami
  • 7,879
  • 12
  • 36
  • 66
0

dp stands for density independent pixels. It works best on wider ranges of screens and different densities out there. It automatically scales based on the device its been on

johnrao07
  • 6,690
  • 4
  • 32
  • 55