Physical size of font in points in Android

Yuri Ushakov picture Yuri Ushakov · Aug 3, 2010 · Viewed 8.7k times · Source

I have two devices - HTC Tattoo and Sony Ericsson Xperia X10. One has 145 DPI, the other 245 DPI.

When I specify font size for a TextView in points, like this:

textView.setTextSize(TypedValue.COMPLEX_UNIT_PT, 6.5f);

I get different physical size of text on these two devices. On 245 DPI, the text is barely readable.

Now, "pt" size is supposed to be physical. I.e., in my test, both text blocks should have letters of the same physical height. Which is not the case.

What can be wrong here?

Thanks for the help, Yuri.

Answer

Nick picture Nick · Aug 3, 2010

The pt unit is points, which will not scale according to density. For non-text you want dip (or dp), which are density independant pixels. These will scale according to density. For text, you want sp, which will scale according to density but also according to the user's preferred font size.

There's a little info on it here, and also scattered elsewhere in the Android docs.

http://developer.android.com/guide/practices/screens_support.html#screen-independence

UPDATE: here are descriptions of each of the dimension units, which as Yuri rightly points out does suggest that pt should always be 1/72 of an inch on the physical screen. It just doesn't appear to work that way, and the answer is just to use dp and sp - if you want to expose something like points to your user, just do some maths (dp can be assumed to be 1px on a 160dpi screen, i.e. 1/160th of an inch).

http://developer.android.com/guide/topics/resources/more-resources.html#Dimension