0

So I got this font that must be used at a size of 9 pt in my app. This is what my designer, an Android illiterate, is telling me. No problem:

9 pt = (1 / 72) * 9 = 1 / 8 in.

@ 285 dpi, this means 9 pt = 285/8 px

Being xhdpi, 9 pt = 285 / 16 dp.

Neat! Now how do I convert this value to sp so I can feed it to the method from the title ?

Thanks

kellogs
  • 2,837
  • 3
  • 38
  • 51
  • 1
    get another designer... or get through this post and the answers: http://stackoverflow.com/questions/6656651/difference-between-android-dimension-pt-and-dp – WarrenFaith Aug 30 '12 at 21:37

1 Answers1

1

It's not specified in the documentation, but I'm fairly certain Paint.setTextSize() accepts a pixel value, not an sp value, in which case you simply need to pass in the pixel dimension (285/8). The problem would be trying to get your exact density which, from what I can gather is not possible. You would have to go with your density "bucket" value (e.g. hdpi = 240).

EDIT: If you're actually talking about TextView.setTextSize() then you could actually just use setTextSize(TypedValue.COMPLEX_UNIT_PT, 9) and skip the whole conversion problem.

Community
  • 1
  • 1
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • uhm, perhaps this is a rushed reply. See your other response at http://stackoverflow.com/questions/5032355/android-textview-settextsize-incorrectly-increases-text-size. And no, the exact DPI is not a problem since no such accuracy is required in my case. But this is anther story. So sp or px ? – kellogs Aug 30 '12 at 22:39
  • 2
    That's different for TextView. TextView does take sp units by default, but the Paint class I'm pretty sure uses pixels. – Kevin Coppock Aug 30 '12 at 23:01
  • Isee now. How about the Paint.getTextSize() ? Does that work in px too ? – kellogs Aug 30 '12 at 23:48