Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top