Why did I have this Alert message in my xml layouts in android
the message is Avoid using "px" as unite, use "dp" instead
The answer is right there: What is the difference between "px", "dp", "dip" and "sp" on Android?
pixels is the exact number, and dip is dynamic: better to keep the view you want on any type of screen
It's a best practices thing.
dp = Density-independent pixel
A pixel is not a pixel is not a pixel. Some screens have 72 pixels per inch some have 200+
Read more here: http://developer.android.com/guide/practices/screens_support.html
DP should always be used as the layout will better scale for devices that have lower/higher DPIs. If you use px for the layout its a fixed will use this many pixels, therefore the layout component could either be really small or massive and not look good.
dp should always be used as it handles the scaling for various devices much better.
Check http://developer.android.com/guide/practices/screens_support.html for more info.
Check out these developer docs:
http://developer.android.com/guide/practices/screens_support.html#density-independence
http://developer.android.com/training/multiscreen/screendensities.html
Related
I'm having problems with images on android, I need to create one image that have the same size in different devices but I don't know how I calculate this using DisplayMetrics.
How can I set the same size in different width and DPI devices?
Short answer: use an amount of dp (density independent pixels) as width and it should look the same on different devices.
To get a better understanding of screen sizes I would recommend reading this article from Google Developers https://developer.android.com/guide/practices/screens_support.html
I think you mean using "in" (inch) instead of dp/dip and worth to know those differences between measurements in android from this question
If you want to display the image in same size in different devices, perhaps you should use Imageview for that, it will show you the image exactly in same size and position in every device.
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.
i'm trying to understand if the margin values of the Relativelayout in android are in pixels or dpi ?
thanks.
In xml you specifies measuring units such as android:layout_marginTop="13dp". See documentation about measuring units.
In code you specifies masuring units in pixels (for example margins), but you can read dimensions from resources, where you can specify dimension in dp or sp, etc.
You can use both pixels and dpi. But if you want to make your application for multiple screen support then you have to prefer dpi. Because it will adjust the size according to screen size.
You can choose. Enter either "5px" or "5dp", and it will adapt itself.
I guess you are refering to the values of certain methods. they are in pixels so you better convert the dpi values to pixels using displaymetrics
I am trying to understand the "Best practices for Screen Independence".
Can someone explain me when to use and when not to use the DP and PX measures ? (of course it is in documentation but my understanding is little vague from it..)
Thanks
I tend to use DP at all times. Avoid PX like the plague.
Have you read Supporting Multiple Screens? Essentially, if you want to scale to keep the same relative size on different screens, use DP; if you want to shrink on larger displays (and grow on smaller ones), use PX.
Hope this helps,
Phil Lello
You should always use dp as the format of any height and width of the View.
If you want more help to measure the dp and px of the particular resolution then..
see this Link: LINK which helps you to measure the px for the different resolution.
Enjoy Coding... :)
Received an email from my first Motorola Droid user. The new 480x854 resolution introduced in Android 2.0 (as opposed to 320x480) is wreaking havoc with my user interfaces. Everything is smaller and ill-positioned.
I was under the impression that if we follow the XML layout guides we were resolution-safe, as no absolute coordinates are used. Does anyone have experience in making the UI resolution-safe? Will we need a main.xml for each resolution times each orientation?
Which dimension units did you use?
AFAIK using dp and sp should keep you safe.
From documentation:
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", though
"dp" is more consistent with "sp".
sp
Scale-independent Pixels - this is like the dp unit, but it is also
scaled by the user's font size
preference. It is recommend you use
this unit when specifying font sizes,
so they will be adjusted for both the
screen density and user's preference.
The eclair emulator works wonders for these issues, also make sure to read:
http://developer.android.com/guide/practices/screens_support.html
Your application responds to different resolutions based on many factors, even the min-sdk.
Also, from the page, are some best practices:
Prefer wrap_content, fill_parent and the dip unit to px in XML layout files
Avoid AbsoluteLayout
Do not use hard coded pixel values in your code
Use density and/or resolution specific resources
Aside from the resolution difference, the other thing to consider is that the Droid's WVGA screen has a different aspect ratio from previous devices like the G1. In many older apps that I've downloaded, this manifests as a gap at the bottom of the screen, or elements that are vertically misaligned in portrait mode. You may want to try running your app in the emulator with a WVGA skin to check for any hidden assumptions that your layout makes about the aspect ratio.