What does HeightPixels / Density actually represent? - android

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

Related

what resolution for creating android UI

I read about dp and pixel and calculate dpi and pixel . but i am confused. I want to know if I want to create UI for my app, is better what resolution I select for beginning? I want to use of Photoshop. on design section in Android studio, there are e few devices with their resolutions,for example (480 * 800 hdpi(nexus one)) and so on . but which one is better for first create on photoshop?
I suggest using px for design
Mobile: 360 x 640
Tablet 7inch: 600 x 960
Tablet 9inch: 1024 x 768
References
https://i.stack.imgur.com/rrNoM.png
https://i.stack.imgur.com/ueFa0.png
https://i.stack.imgur.com/CAt1u.png
Use Density-independent pixel (dp) units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
The android documentation says :
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.
You can read it here
For correct icon size you should read answer this

Android - Puzzles about conversion between dp and pixel

I know this might be a silly question but I have really gone through so many materiel and links but still not quite understand it. In the "Supporting Multiple Screens" section of Android Develop Doc, it introduces dp like this:
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.
Basically I understand the fact that screen with higher dpi would have more pixels in a single physical inch, which means a dp in such a screen would equal more physical pixels(px).
But according to the above conversion equation (in bold font), in a screen with higher dpi (e.g. 240 dpi screen) , a px = (240 / 160) * dp = 1.5dp. This seems to mean that in higher dpi screen a px would equal more dp. This looks in conflict with my previous understanding.
So, please, could anyone help me to figure this tricky issue out. Thank you a lot, really.
You are looking at the wrong place in the formula. To see how many dp equals one px in different density, lets rearrange the formula:
px = dp * (dpi/160)
dp = px / (dpi/160)
Now for 1px, in mdpi devices:
dp = 1 / (160/160) = 1dp
In hdpi devices:
dp = 1 / (240/160) = 0.666666667dp
You can see that 1px equals less dp in higher density devices

android the definition density-independent pixels

I am new to android. I read about DP but m still confused. In one definition it says-
dp (density-independent pixels): An abstract unit based on the density of the screen. On a display with 160 dots per inch, 1dp = 1px.
does it mean- 160 dots=1 dp = 1 px (each dot is 1 pixel , right?)
OR 1 dp = 1 dot(pixel) among the 160 dots
Pleas clarify
density-independent pixels 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.
For 160 dpi screen 1 dp equals 1 px.
Refer to this blog and this answer.
The android documentation say -
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.
you may take a look at the supporting multiple screens. you may also take a look at this question.

Android: device independent pixel?

If i have 96dip on a smaller screen device, it will enlarge to keep the ratio to the large screen device.
Am I right?
That's right.
From the developer guide:
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.

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.

Categories

Resources