0

I have an issue regarding usage of "dp".
In which context dp can be used? Is it just used for any controls i.e ImageView and TextView, or it can be used for adjusting layouts as well.
For adjusting layouts I'am currently specifying width and height of the controls in terms of percentage of the screens width and height.

Nickolaus
  • 4,785
  • 4
  • 38
  • 60
sankettt
  • 2,387
  • 4
  • 23
  • 31

2 Answers2

0

it can used as unit to specify width and height.. you can use it with all views...see this for more..

ngesh
  • 13,398
  • 4
  • 44
  • 60
  • this is wat i wanted to know.. i was trying to do the same.. but is it advisable to use percentage of screen width or dp ? – sankettt Apr 18 '12 at 11:01
  • @sankettt .. dp is the way to go.. it specially there to support multiple resolutions and screen sizes.. – ngesh Apr 18 '12 at 11:05
  • thanks.. but when i use it for padding then the images are getting shrinking to which i add padding. – sankettt Apr 19 '12 at 11:17
  • @sankettt .. don't give images fixed width.. say wrap_content – ngesh Apr 19 '12 at 11:25
  • since i am using one medium size image for every screen size and specifying the width and height in terms of dp it scales up and down according to the dpi of various screen. and my question was that i want to achieve images placed like a circle so i would have to provide padding to achieve so in what terms i should specify padding i tried specifying in terms of dp but its getting shrink. – sankettt Apr 19 '12 at 12:36
  • @sankettt .. padding applies to view thats holding it and not to the images.. so when view is padded image has to be confined in it.. first make sure and edit your image.. only way to go..or try using cropping like this http://stackoverflow.com/questions/3846338/how-to-crop-an-image-in-android – ngesh Apr 19 '12 at 12:41
  • hmm yeah now i realised what i was doing thats why i was getting that effect.. thanks a lot..! – sankettt Apr 19 '12 at 12:52
0

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",

The use of dip or dp is to make your Android applications compatible with multiple screen densities and resolutions.

UVM
  • 9,776
  • 6
  • 41
  • 66
  • ohk but then can dp be used to specify padding rather than specifying it in terms of percentage eg..int width=display.getWidth(); txtfuelcurrentkm.setPadding(0,0,(int) (width*0.005),0); .. i am using this now... @UNNI – sankettt Apr 18 '12 at 10:56