Questions tagged [density-independent-pixel]

DIP is short for Density Independent Pixel. It is used by the Android system as a unit of measurement to make GUIs fit on different screen sizes with scaling. This allows the developer to create a single interface for different screen sizes.

119 questions
226
votes
12 answers

How to determine the screen width in terms of dp or dip at runtime in Android?

I need to code the layout of the android widgets using dip/dp (in java files). At runtime if I code, int pixel=this.getWindowManager().getDefaultDisplay().getWidth(); this return the screen width in pixels (px). To convert this to dp, I coded: int…
Khushboo
  • 3,095
  • 2
  • 23
  • 24
161
votes
21 answers

Formula px to dp, dp to px android

I am trying to calculate a variable amount of pixels to density independent pixels and vice-versa. This formula (px to dp): dp = (int)(px / (displayMetrics.densityDpi / 160)); does not work on small devices because it is divided by zero. This is…
Bram
  • 4,533
  • 6
  • 29
  • 41
102
votes
7 answers

Does setWidth(int pixels) use dip or px?

Does setWidth(int pixels) use device independent pixel or physical pixel as unit? For example, does setWidth(100) set the a view's width to 100 dips or 100 pxs? Thanks.
101
votes
7 answers

How to convert DP, PX, SP among each other, especially DP and SP?

I have known the difference among DP, SP and PX. And after searching this topic, I found nothing satisfying me completely. Maybe this post is a duplicate, but I still want to know what is the formula of converting from DP to PX, and DP to SP, from…
SilentKnight
  • 13,761
  • 19
  • 49
  • 78
53
votes
4 answers

Convert dip to px in Android

I had written method to get the pixels from dip but it is not working. It give me runtime error. Actually I was running this method in separate class and initialized in my Activity class Board board = new Board(this); board.execute(URL); This code…
user961524
  • 537
  • 1
  • 7
  • 19
52
votes
4 answers

What is the correct way to specify dimensions in DIP from Java code?

I found that it is possible to set dimensions of my interface elements in XML layouts using DIPs as in following fragment: android:layout_width="10dip" But all Java interface takes integer as arguments and there is no way to specify dimensions in…
Pavel Lahoda
  • 219
  • 1
  • 4
  • 7
30
votes
6 answers

How do dp, dip, dpi, ppi, pixels and inches relate?

I was reading dp, dip, px, sp measurements, but I still have some questions about dp/dpi vs ppi vs px vs inch. I am not able to compare them... is an inch the largest? They say 160 dpi means 160 pixels per one inch. Does that mean 1 inch contains…
Rebooting
  • 2,762
  • 11
  • 47
  • 70
28
votes
3 answers

How do you make layouts for several Android screen sizes?

I've done some research on building layouts that work for multiple screen sizes and I'm looking for some clarification. Is it common practice to just make a separate layout file for each of the three screen sizes (small, medium, large) or can you…
joepetrakovich
  • 1,344
  • 3
  • 24
  • 42
15
votes
2 answers

WPF Units and Code-Behind

Recently I discovered WPF supports different measurement units in XAML. Besides default DIPs, there is also support for pixels, inches and centimeters (as far as I know). This allows designer to write XAML such as this:
13
votes
2 answers

How do I figure out what size (in pixels) an image needs to be for Android to display that image correctly across devices?

I have two images I want to display within my app. The first image is to be full screen (as a background for the app). I have read the 'Supporting Multiple Screens' article on developers.android.com multiple times but, I am still at a loss as to…
netman74501
  • 151
  • 1
  • 9
12
votes
4 answers

How to convert dp to px in xamarin.android?

I want to convert dp to px in my C# code in xamarin.android, but all I could find were java codes in android studio that have some problems in xamarin. I tried to use equivalent like using Resources instead of getResources() and I could solve some…
9
votes
2 answers

Android Compose set height of view in pixels NOT in dp

I want to set the height of view in pixels not in dp. `Box(modifier = Modifier.height(100.dp))` In this example height of the box is set to 100 dp and modifier function accepts only dp. How to set height of Box in pixel?
9
votes
1 answer

Maximum dp/dip on android devices

Since dip stands for "device independent pixels", is there a standard number of dip for every android device out there? Example: if a hdpi device has X dp/width and Y dp/height, does a ldpi device have the same values? Namely X and Y? If so, can…
kimv
  • 1,569
  • 4
  • 16
  • 26
7
votes
4 answers

Is ConstraintSet.connect() margin parameter in px or dp?

val margin = 8 ConstraintSet().apply { connect(anId, ConstraintSet.START, anotherId, ConstraintSet.START, margin) } Is margin applied as pixels or density-dependent pixels? Various articles scattered around the web and apocryphal knowledge seem…
nukeforum
  • 1,284
  • 3
  • 16
  • 38
7
votes
4 answers

basics of device-independent-pixels

im throughoutly confused by dips on Android. I understand from the reference that the base for dp values is 160. So, shouldn't 80dp in width equals a view with a width of 50% of the screen ? On my Nexus One the width in dp is something around 300dp…
user577708
  • 71
  • 1
  • 1
  • 2
1
2 3 4 5 6 7 8