5

I don't know how it convert to the unit of pixels. For example, if I assign 10sp to my font size on 160 dpi device, how much px of my font size is it ? Or, if I assign 10sp to my font on 240 dpi device, how much px of my font size is it ?

AechoLiu
  • 17,522
  • 9
  • 100
  • 118

2 Answers2

7

They're just the same as dip, but they also take into account the font scaling factor that the user sets on his device. So if he/she left font size set to "normal", it's just like dip. If there is a font scaling factor, you can get the whole scaled density from DisplayMetrics.scaledDensity.

(I have to admit I just tried to look for some "font size" option in my Android phone global settings and I couldn't find it, so I wonder if it is of any use right now. Am I missing something?)

bigstones
  • 15,087
  • 7
  • 65
  • 82
  • 1
    So, the sp is equal to dp * DisplayMetrics.scaledDensity ? If the font size set to "normal", then I can assume the DisplayMetrics.scaledDensity is 1.0 . – AechoLiu Mar 13 '11 at 23:00
  • @Toro mm I never checked but by what ref says I think sp = scaledDensity = text_scaling * dp – bigstones Mar 13 '11 at 23:07
  • 1
    At least on stock ICS you can use either "Settings > Display > Font size" or "Settings > Accessibility > Large text" to change font size (the former lets you choose any of [Small,Normal,Large,Huge] from a menu; the latter is just a checkbox to toggle between Normal and Huge). – John Mellor Mar 02 '12 at 16:42
  • @bigstones Not all ROMs allow you to change the font size, but as John noted, at least stock ICS does. – kabuko Apr 26 '12 at 21:39
  • 1
    The option to change the font preferences came only with Android 4.0. So it depends on the OS used. @toro: don't assume — Touchwiz does not have a normal between small and medium large. Instead it has 6 options (two more then stock Android) – Martin Oct 29 '13 at 09:11
0

This depends on screen density set for the device. You can obtain it with

screenDensity = this.getResources().getDisplayMetrics().density;

Then by multiplying screenDensity and the size in dp or sp you can get actual pixel size. For example on 320x240 device the creen density is about 0.75, while on HTC Desire HD and Galaxy Tab is 1.5 even though they have different resolution and may not have the same dpi.

Lumis
  • 21,517
  • 8
  • 63
  • 67
  • I think sp is not the same as dp, and I still can not get a clear image about how sp works from your answer. – AechoLiu Mar 13 '11 at 23:03
  • sp will take the user scale preference in account, which means if a user wants 20% bigger fonts this will be added to the dp units of the font. – Lumis Mar 14 '11 at 14:40
  • As @bigstones pointed out: it is `scaledDensity` — `density` give you dp and not sp. And since Android 4 they are not the same any more. – Martin Oct 29 '13 at 09:17