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!
Related
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
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'm using dip as unit in my apps. I figured out, that width isn't exact on different devices, for example: 292 dip width will cause different results on 160 and 320 dpi devices.
This is my basic approach:
<ImageView id="panelImage" width="292" image="/images/loginPanel.png"></ImageView>
Before showing the element I check the device density factor (with Ti.Platform.displayCaps.logicalDensityFactor) and upscale the image width with 160/Ti.Platform.displayCaps.logicalDensityFactor like this:
$.panelImage.width = $.panelImage.width * 160 / Ti.Platform.displayCaps.logicalDensityFactor;
It works, but my problem is that I have to recalculate size values on EVERY item...
Is there any other solution for this?
The best would be something like this (BUT ITS NOT WORKING!!!):
<ImageView id="panelImage" width="(292*160/Ti.Platform.displayCaps.logicalDensityFactor)" image="/images/loginPanel.png"></ImageView>
(So, I try to use the multiplication in Alloy View definition XML file.)
Any idea?
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.
There are a lot of questions on this on here, but none that answer my specific question. When I use px, eclipse tells me to avoid it. So I did. But when I use dp, the grid/borders I am making around my cells in GridView seems too fat. I want it much thinner. When using 1 px as opposed to 1dp, I get the desired result. I understand warning on avoiding it, but should I really avoid it in this case?
Yes you should. "dp" will give pretty the same look on all devices, but pixels are not.
http://developer.android.com/guide/practices/screens_support.html
Long story short:
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
px doesnt work on all devices i think it stopped working in android 2.2
I recommend sticking with dp.