1

I'm trying to convert some text dimension from a PSD representation to Android DP format. All over the internet there are resources that explain what are DP, SP an how to convert them but I still don't understand what exactly to do. I need an example for my case.

Let's say I have PSD with 3 lines of text with different dimensions:

18px font - ? dp
21px font - ? dp
28px font - ? dp

Which dimensions to choose for a handheld device if I want to maintain roughly the same apparent dimension? How to make my GUI to look similar to my PSD template?

shadox
  • 3,128
  • 4
  • 23
  • 37
  • You can find some converters out there. Like [this](http://labs.rampinteractive.co.uk/android_dp_px_calculator/) one. !OPS! you should use `sp` for font sizes – Detilium Aug 25 '15 at 09:29
  • dp/sp are pixel density dependant sizes. that means than on 10 different devices, all with 18dp text, each dp will have a different pixel value. don't work with absolute values (px). – CptEric Aug 25 '15 at 09:32
  • Look a this answer: http://stackoverflow.com/questions/2025282/difference-between-px-dp-dip-and-sp-in-android – M Penades Aug 25 '15 at 09:32
  • http://stackoverflow.com/questions/2025282/difference-between-px-dp-dip-and-sp-in-android – Anis LOUNIS Aug 25 '15 at 09:37
  • I know how to use Google, find converters, find other stackoverflow answers. What I don't fully understand is how to convert it well. Converters usually say that 14dp is 14px on mdp some more on hdpi, a lot more on xhdpi and so on. It's not what I'm looking for. – shadox Aug 25 '15 at 09:42

1 Answers1

0

Taken from this SO thread

  • px is one pixel.
  • sp is scale-independent pixels
  • dip (dp) is Density-independent pixels.

Use

  • sp for font sizes
  • dp for everything else

From Android Developer

+---------+-------------+---------------+-------------+--------------------+
| Density | Description | Units Per     | Density     | Same Physical Size | 
|         |             | Physical Inch | Independent | On Every Screen    | 
+---------+-------------+---------------+-------------+--------------------+
| px      | Pixels      | Varies        | No          | No                 | 
+---------+-------------+---------------+-------------+--------------------+
| in      | Inches      | 1             | Yes         | Yes                | 
+---------+-------------+---------------+-------------+--------------------+
| mm      | Millimeters | 24.5          | Yes         | Yes                | 
+---------+-------------+---------------+-------------+--------------------+
| pt      | Points      | 72            | Yes         | Yes                | 
+---------+-------------+---------------+-------------+--------------------+
| dp      | Density     | ~ 160         | Yes         | No                 | 
|         | Independent |               |             |                    | 
|         | Pixels      |               |             |                    | 
+---------+-------------+---------------+-------------+--------------------+
| sp      | Scale       | ~ 160         | Yes         | No                 | 
|         | Independent |               |             |                    | 
|         | Pixels      |               |             |                    | 
+---------+-------------+---------------+-------------+--------------------+

px Pixels - corresponds to actual pixels on the screen.

in Inches - based on the physical size of the screen. 1 Inch = 2.54 centimeters

mm Millimeters - based on the physical size of the screen.

pt Points - 1/72 of an inch based on the physical size of the screen.

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.

Check out this SO thread aswell. (Changing px to dp programmatically)

Community
  • 1
  • 1
Detilium
  • 2,729
  • 7
  • 26
  • 60
  • Again, it's not what I'm looking for. I don't need to convert px to dp programatically. I just need to figure out how to convert a fixed design template made in Photoshop, which is in pixels, to a device independent template, which should be in dp/sp. This is just a matching table. – shadox Aug 25 '15 at 10:26
  • Then I must admit that I don't know what you need – Detilium Aug 25 '15 at 10:34