views:

40

answers:

2

Hello.

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.

+2  A: 

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

Nick
According to the doc: "Points - 1/72 of an inch based on the physical size of the screen." No matter what density. No matter anything. Just like a millimeter is always a millimeter on any display. 1/72 of an inch is 1/72 of an inch, no matter how many pixels it will take.Let me explain about my choice. Points are the standard font unit for many applications. Users do not want to specify their font sizes based on some new measurement unit. Points are standard and commonplace. In your PC browser, Microsoft Word, Notepad and Mac applications.
Yuri Ushakov
Yes, the documentation does read as though pt should always be the same physical size, it is a bit misleading. But dp and sp do seem to be the recommended units. 1dp is 1px on a 160dpi screen (i.e. it is 1/160 of an inch), so if you want to expose a points equivalent to your users, it would just be a simple mathematical conversion.
Nick
+1  A: 

Actually, points always work whenever the underlying devices gives correct display metrics.

For example, I tested five devices:

NAME      OS    metrics.ydpi  REAL_DPI  DDPI  SD
XPERIA    1.6   159.49677     325       240   1.5
Liquid    1.6   263.8958      285       240   1.5
Tattoo    1.6   145.14067     match     120   0.75
Hero      1.5   179.29352     match     -     1.0
Galaxy    1.6   160.41878     180       160   1.0

As can be seen, only HTC Tattoo and HTC Hero give proper display info to Android API.

This is why points have different physical sizes on different devices (not all, though).

The only way I found to roughly scale fonts / images is to assume everything is 160 DPI and use DisplayMetrics#scaledDensity - which is wrong, but I don't see any other way.

Yuri Ushakov