This question already has answers here:
Can Android support dp's less than 1dp?
(3 answers)
Closed 5 years ago.
1dp is one pixel for a display with a ppi of 160. Why does changing that to a small number (0.3) does not change the visibility? I was expecting that changing 1dp to 0.3 dp will make the widget(just a line) invisible.
In android there is a formula:
px = (int)(scale * dp + 0.5)
(i.e. the device density scale rounded to the nearest whole pixel value)
The scale value would be based on the screen density of the device:
So 0.5dp would result in {1px, 1px, 1px, 2px} for the above densities, whereas 1dp would be {1px, 2px, 2px, 3px}. A tiny value like 0.1dp would resolve to {1px, 1px, 1px, 1px} because anything less than 1 in the above formula resolves to a single pixel unless the value was explicitly 0dp (it's not possible to draw something thinner than a single pixel width).
If you want to ensure that thinnest possible width is used, probably best to define the width explicitly with px instead of a scaled unit like dp. Setting a value of 1px will draw with a single pixel on all devices, and is much more readable than hoping 0.5dp or 0.1dp does the same.
For more details read the documentation
Related
If I wanted to create something that is exactly one inch apart on every Android screen, I would use pixels correct? Inches and dp seem to scale to the individual size of the screen meaning on one device it could be one inch and on another it could be 1 1/4th.
Also, is there an easier way to do this besides finding how many pixels are in an inch and then adding the views with the correct margin pragmatically?
You would use density independent pixels. Also known as dip or just dp.
Dp ensure that things are nearly the same size on every screen. ("Nearly" because this is android, with a huge fragmentation, and custom roms, ...etc)
So one inch will be a varying amount of pixels on each screen. If you use pixels, things will be smaller on high resolution displays. So use dp and everything will be the correct size on the actual screens.
While using dp might be off by a pixel or two (might!), this is probably as good as it gets. Also, you should try not to create pixel perfect design with android—You will have a hard time with different screen sizes (and not just resolutions)
Generally speaking you should try and follow the material design guidelines by google for android, and rather use multiples of 8dp for sizes, instead of trying to find out how much an inch is.
Density-independent pixels (dp) is the unit of measure which appears to be the same actual length on any device.
Try this: create a view whose width is 100dp and run it on a hdpi device. Measure the width of the view using a ruler or something. Then run the app on a mdpi device and measure the width. You will find that the two widths are about the same.
This means that, just as its name suggests, the actual length of a dp is independent of the screen density.
If you used pixels, however, the actual length will vary from screen density to screen density. If the screen is denser, 100px will appear shorter. If the screen is less dense, 100px will appear longer.
Get it?
tl;dr: Just use dp!
I would like to get size of the Android Phone but size in mm and not in pixels.
I know how to get size in pixels, however I would like to do some RND and for that I want to get width of the screen in mm.
Like here Galaxy S4 size is Dimensions 136.6 x 69.8 x 7.9 mm (5.38 x 2.75 x 0.31 in)
136.6 x 69.8 x 7.9 mm (5.38 x 2.75 x 0.31 in)
^^^
Does any one knows what is this size for?
Though I am iPhone developer BUT I am beginner (1 day baby) for Android. Well below is what I want to do as RND.
Let's say designer gives me design for the app of size 1080*1920 and have title of size 30px (which covers 80% exactly) of the screen size.
That means 10% space on left side, 80% text and 10% space on right side.
I know in Android we have something called sp or android:textAppearance="#android:style/TextAppearance.Large", but that won't give the 10% spaces.
What I want to achieve is regardless of size, design should go same.
Considering design to fit with width, if height increases, I would have scrollview and I am okay with that.
"Pixel density" is the number of pixels per inch. So dpi gives you the scale factor you need so that the number of pixels is consistent across devices.
Another way of saying this is that if you have a 480 pixel screen with hdpi, then a 360 pixel screen at mdpi is about the same physical size. While the screen sizes may vary some, it generally is not that important. Even so, as mentioned, see this post about doing the actual calculation:
Is there a way to determine android physical screen height in cm or inches?
However, you are talking about percentages of screen size in your example. If you know the screen size in pixels, then the physical size doesn't matter, just do your calculations using pixels. If you have 480 pixels, 10% is 48.
You cannot do this in XML, which it sounds like you might be wanting to do. You can use "weighted linear layouts" for runtime calculations of the screen size. You can also use different res folders depending on screen size (like res/layout-hdpi).
You may want to read this from Google regarding supporting different screen sizes: http://developer.android.com/guide/practices/screens_support.html
Use DisplayMetrics. The API is: http://developer.android.com/reference/android/util/DisplayMetrics.html
You can perform a calculation on the given fields and obtain the height and width in inches, then convert that to mm.
This question already has answers here:
What is the difference between px, dip, dp, and sp?
(32 answers)
Closed 8 years ago.
I am specifying a button's width as 600dp in values/dimens.xml, but when I deploy and run it on my Nexus 5 it takes up all the available width in the landscape orientation.
My activity only runs in landscape mode, as I have specified in manifest file. The layouts are in the layouts directory. The images are in the 'drawable-xxhdpi' directory and they appear fine, but everything that I specify in dp is magnified.
The previews in Android Studio and the actual device seem to behave as they have 600dp max width when it should be 1920. Why is this, and how can I fix it?
The physical width of the display on a landscape Nexus 5 is 1920 pixels, However, 600dp != 600px. The Nexus 5 has a display density of ~445 ppi. A dp is pixels only at medium density (160 ppi). Thus, everything specified in dp will be scaled by a factor of about 445/160 = 2.78125. So 600dp is actually about 1669 pixels. That should account for most of what you're seeing.
There may also be something about your layout that is stretching the button. If you post your layout xml, we might be able to provide more info.
I am making a calendar and I need a grid. Problem is the borders, or rather the space between each grid, which is what I am using to kind of simulate a grid, is 1 dp. But its rather thick. I am looking at other calendar apps that have the borders, and they are very thin. Even if I were to use a drawable shape and make it 1dp, it still has that thickness. I tried using .5, but that did not seem to work. Is that not possible?
Android does allow you to enter fractional values for dp into XML, though I'm not sure I would recommend it because the results become less easy to predict/compute. Devices convert dp values into pixels using (basically) the following formula:
px = (int)(scale * dp + 0.5)
(i.e. the device density scale rounded to the nearest whole pixel value)
The scale value would be based on the screen density of the device:
MDPI = 1
HDPI = 1.5
XHDPI = 2
XXHDPI = 3
So 0.5dp would result in {1px, 1px, 1px, 2px} for the above densities, whereas 1dp would be {1px, 2px, 2px, 3px}. A tiny value like 0.1dp would resolve to {1px, 1px, 1px, 1px} because anything less than 1 in the above formula resolves to a single pixel unless the value was explicitly 0dp (it's not possible to draw something thinner than a single pixel width).
If you want to ensure that thinnest possible width is used, probably best to define the width explicitly with px instead of a scaled unit like dp. Setting a value of 1px will draw with a single pixel on all devices, and is much more readable than hoping 0.5dp or 0.1dp does the same.
Yes.
I set to 0.1dp and it is smaller than 1dp on my 4" ME860 DRIOD.
Simple answer is yes, you can.
Simpe solution to your problem (i.e. achieve the thinnest possible line) set thinckness to:
1px
I suggest px as its not device dependant and it is the smallest measure available, furthermore it wont be converted into another form, therefore no rounding errors nor any unexpected rendering... let me explain.
Android layout xml file, or dimensions xml values file, will allow you to enter decimal values for dp.
HOWEVER:
I have experimented with several values from 0.1dp to 1dp, on many devices. Depending on the device - it may not render so as you expect.
On lower pixel density devices, the lines may render thicker on one side compared to another side even though they are coded to have the same... This is due to inaccuracy introduced when truncating the converted to dp to px value - (as Devunwired mentions):
px = (int)(scale * dp + 0.5)
A pixel (px) is the smallest unit and must be an integer. so - if your aim is to simply have the smallest possible line/border, why not set it to:
1px
I hope this helps!
This question already has answers here:
What is the difference between px, dip, dp, and sp?
(32 answers)
Closed 4 years ago.
The first problem is the proportions in the images. Someone can tell me concordance between dip and pixel? Thanks!
You really shouldn't ask questions like this before checking for similar ones. It takes you 10 secs to google this btw.
"Density-independent pixel (dp):
A virtual pixel unit that applications can use in defining their UI, to express layout dimensions or position in a density-independent way.
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, the baseline density assumed by the platform (as described later in this document). At run time, the platform transparently handles any scaling of the dp units needed, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: pixels = dps * (density / 160). For example, on 240 dpi screen, 1 dp would equal 1.5 physical pixels. Using dp units to define your application's UI is highly recommended, as a way of ensuring proper display of your UI on different screens."