Android: 50dip is different size on different devices? - android

My understanding of the density independent pixel, is that it will render as the same size regardless of the screen dpi and dimensions. I created an image button of with and height of 50dip. On my medium dpi Samsung Galaxy Tab, this button measures about 10mm x 10mm (measured with a ruler. On my large, hdpi Acer Iconia Tab, the button measures about 7mm x 7mm. I would have expected the two buttons to have the same actual size. Am I doing something wrong, or is my understanding incorrect?

Both previous answers are correct. The problem lies in that not only changes the size in inces, but also the number of total pixels.
For a solution, you will need to create different xml layout files in res/layout-normal and res/layout-large, to adjust for screen resolution changes. Still, given that these folders group ranges of devices, you will have some variation in size.
Alternatively, if you strictly need a specific fixed size for a View, set up its width and height as "10mm"
public static final int layout_width Since: API Level 1
Specifies the basic width of the view. This is a required attribute for any view inside of a containing layout manager. Its value may be a dimension (such as "12dip") for a constant width or one of the special constants.
May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).
http://developer.android.com/reference/android/R.attr.html#layout_width
For a list of screen sizes, resolutions and dpi values, take a look at: http://en.wikipedia.org/wiki/List_of_displays_by_pixel_density
To calculate the real dpi value, check here: http://en.wikipedia.org/wiki/Pixel_density#Calculation_of_monitor_PPI

You've got it the wrong way round, the Galaxy Tab is HDPI and the Acer Tab is MDPI.
50dips is 50 pixels on an MDPI device or 75 pixels on an HDPI device, which should translate to roughly the same physical size.
However things are slightly different due to the much larger screen size on 3.0 tablets. In these cases often using the HDPI assets gives a better size.

from: http://developer.android.com/guide/practices/screens_support.html (emphasis added)
Density-independent pixel (dp)
A virtual pixel unit that you should use when defining UI layout, 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, 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.

Related

What does HeightPixels / Density actually represent?

I am looking at setting the FontSize of my application based on how big my device is. i.e. If the device is below 8" make the font smaller than the default.
So the device I am using is a Lenovo S8-50 8-Inch Tablet and as you can see in the specifications Screen Size: 8 inches
So looking at some code on Github I see the following:
var d = Resources.System.DisplayMetrics;
this.ScreenHeight = (int)(d.HeightPixels / d.Density);
For this device the number returned is 912 and I can't figure out how this number relates to the device.
So my question is what does this number actually represent?
From the Official documentation:
Density-independent pixel (dp)
A virtual pixel unit that you should use when defining UI layout, 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, 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.
This will also be helpful:
Understanding density independence

Use of dp and dpi instead of percentages in android?

I have understood the concept of dp and dpi for different layouts and images being used in android. But my doubt (sorry if dumb) is why did android come up with a concept of density pixel, density independent pixel instead of percentages just like in html. It is getting difficult for the developer..
Because of to support multiple screens android introduced this dp and dip concepts
dp or dip
From the Android Documentation for Supporting Multiple Screens:
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.
Use of dp:
Density independence - Your application achieves “density independence” when it preserves the physical size (from the user’s
point of view) of user interface elements when displayed on screens
with different densities. (ie) The image should look the same size
(not enlarged or shrinked) in different types of screens.
Density-independent pixel is a virtual pixel unit that you should use when defining UI layout, 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, 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.

Android multiple screen support

I am confused about android's multiple screen support. http://developer.android.com/guide/practices/screens_support.html I read this article, but still something is not clear for me.
I used dp instead of px in my layout
I put high, medium, low version's of an image to drawable directories.
I made this changes according to this article. But in some densities, there is still problem although some of them work very well.
The question is what is the exact width and height in dp units for variety of android screen types. if it is changeable, what is the difference between px?
px is changeable, dp is changeable too??? what is the difference??
if changeable, should I change the view's width and height by code on Create function or create seperate layouts for each screen dentisies? Please give a way to understand this...
Thanks in advance..
px are not changeable. dps or dips are.
To calculate how many px your object specified in dps will be use the formula below:
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 is a fixed measure. This means that if 100px on a small screen take up 1/2 of the screen, it will take up much less on a large screen. dp = density (independent) pixels, is based on the densitity of the device. So if you specify a width to 50dp on a small screen, it will expand on a large screen. Note that dp is not an insurance of layout compability on all devices, since devices have different aspect ratios. To build a perfect layout, that looks exactly the same on all devices, you must use more techniques. Linearlayout allows you to assign weights. Look into that. http://developerlife.com/tutorials/?p=312

How do I convert to DIP?

What is the formula for converting regular pixels to DIP?
Assuming I have a photoshop document with a design in it that was intended for the Galaxy Tab (for example) which is 600x1024 actual pixels.
What is the ratio between those and the DIP? I'm unclear on how Android translates this.
I want to use DIP (rather than pixels) so that it scales to look "ok" on other devices, but my primary concern is to get it pixel perfect in this resolution and my aim is to measure the position of an element in photoshop and then get an exact translation as to what the DIP needs to be for it to lay out identically on the Tab.
Their formula is somewhere in the docs:
truePixels = DIPs * (device DPI / 160)
Ah, there it is:
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.
Taken from here.

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 as it seems.
What am i missing here ?
thx in advance
"dp" == "Density-independent Pixels" (This is also why it was earlier called "dip", though I prefer to use "dp" these days.)
Think of it like other units -- "in" (inches), "mm" (millimeters), etc. It allows you to provide a size that is scaled based on the density of the screen.
We define mdpi to be the base density, so "10dp" on an mdpi screen will result in exactly 10 pixels. On an hdpi screen it will result in 15 pixels, because hdpi is 1.5*mdpi.
Note that though the constants for various densities are similar to DPI (mdpi is 160, etc), density is not exactly DPI. It is an abstract scaling factor that adjusts for screen dpi, but does not try to exactly reflect it. (You would use "in", "mm", etc for exact sizes but 99.9% that is not what you want so stick with "dp".) This greatly simplifies life for everyone because you don't need to deal with many Android devices having a slightly different amount of space for its UI because they each of slight different screen DPIs. Also, device manufacturers can select the density of their device to achieve a desired UI -- for example the Samsung Tab uses a density that is a fair amount larger than the actual DPI, resulting in an overall larger UI.
160 dots per inch. So 80dp would be 1/2 an inch, roughly.
I don't understand your question completely but I suggest you take a look at this, if you haven't already.
http://developer.android.com/guide/practices/screens_support.html
pixels = dps * (density / 160)
Density independent pixels (short: dp) are a virtual pixel unit that will be determined at the runtime of your application.
Formala:
1 dp = 1 Pixel on 160 dpi screen.
So 160 dpi is the baseline density for the system.
The conversion of dp units to screen pixels are quite simple.
Actual device pixels (px) = dp (1) * (dpi (of the device) / 160(baseline) )
For the sake of simplicity: px = dp * (dpi / 160)
Example:
If a 240 dpi device starts your app, then 1 dp equals to 1,5 actual device pixels.
Conclusion:
Dp automatically handles any scaling to bigger or smaller devices. The times where you hardcode the pixels are over. DP ensures the proper scaling on different screen densities.

Categories

Resources