does Relativelayout in Android works in pixels or dpi? - android

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

Related

Which is standard scaling of Android TextView sp or dip

Hi Iam developing android Titlebar , so I need to clarify which is use to android text either sp or dip
or
Which is standard and what is the difference between sp and dip? Thanks!
SP is always used for textSize
DP is used for pretty much everything else
See "Supporting Different Densities" for more info.
From the docs:
One common pitfall you must avoid when designing your layouts is using absolute pixels to define distances or sizes. Defining layout dimensions with pixels is a problem because different screens have different pixel densities, so the same number of pixels may correspond to different physical sizes on different devices. Therefore, when specifying dimensions, always use either dp or sp units. A dp is a density-independent pixel that corresponds to the physical size of a pixel at 160 dpi. An sp is the same base unit, but is scaled by the user's preferred text size (it’s a scale-independent pixel), so you should use this measurement unit when defining text size (but never for layout sizes).
sp is used for text size and dp for everything else. These are mainly to achieve the best support for multiple screens with different sizes and density

Android: Is line length in PX or DIP?

I have a custom view.
When I draw a line from (x,y) to (x+10,y), is the distance in DIP or PX?
As documentation says,
At runtime, the system transparently handles any scaling of the dp
units, as necessary, based on the actual density of the screen in use.
Thus, all units are in dp.
If you are using one of the Canvas.drawLine methods it is in PX
I believe it's in pixels. If you want to know how to convert from DP to pixels as in if you want your custom view to work with DPs just like all other Views.
Converting pixels to dp

Avoid using 'px' as units, use 'dp' instead

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

How to draw a graphical element so that its size stays always the same on each machine?

I need to draw a square having the same size on each device. I am planning to draw a graphical button and I don't want it to be much smaller (or bigger) than an average finger.
Is there any way to ensure that an element I draw (square in this case) doesn't get resized? I need it to be always the same size in centimeters on inches.
Regards,
M.K.
on each device compute the graphic's dimensions as: height/density, width/density and other dimensions you have
That is what the DP unit is made for.
This is a very interesting lecture which also shows what happens if you use px instead of dp: you get different UI sizes on different devices:
http://developer.android.com/guide/practices/screens_support.html
(the specific paragraph is Density independence)
You may also want to take a look at android attributes android:minWidth and android:minHeight

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

Categories

Resources