Why Should We Use Sp for Font Sizes in Android

Why should we use sp for font sizes in Android?

The dp has constant ratio transition to px: dp = px * ratio. Where ratio will never change on any particular device.

While sp (s for scaled) has scalable ratio: sp = px * ratio * scale. Where ratio never changes, but scale is user configurable. This scale can be used by people who need larger font sizes, for example, to use device more comfortably.

For setting font size what is most preferred in android sp, px or pt

sp: Scale Independent Pixel, scaled based on user’s font size preference.Fonts should use sp.

sp is preferrable for setting font size.

What is the difference between px, dip, dp, and sp?

From the Android Developer Documentation:


  1. px

    Pixels - corresponds to actual pixels on the screen.


  2. in

    Inches - based on the physical size of the screen.

    1 Inch OR 2.54 centimeters


  3. mm

    > Millimeters - based on the physical size of the screen.


  4. pt

    > Points - 1/72 of an inch based on the physical size of the screen.


  5. dp or dip

    > Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160
    dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".


  6. sp

    > Scaleable Pixels OR scale-independent pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommended you
    use this unit when specifying font sizes, so they will be adjusted
    for both the screen density and the user's preference. Note, the Android documentation is inconsistent on what sp actually stands for, one doc says "scale-independent pixels", the other says "scaleable pixels".

From Understanding Density Independence In Android:

















































Density BucketScreen DensityPhysical SizePixel Size
ldpi120 dpi0.5 x 0.5 in0.5 in * 120 dpi = 60x60 px
mdpi160 dpi0.5 x 0.5 in0.5 in * 160 dpi = 80x80 px
hdpi240 dpi0.5 x 0.5 in0.5 in * 240 dpi = 120x120 px
xhdpi320 dpi0.5 x 0.5 in0.5 in * 320 dpi = 160x160 px
xxhdpi480 dpi0.5 x 0.5 in0.5 in * 480 dpi = 240x240 px
xxxhdpi640 dpi0.5 x 0.5 in0.5 in * 640 dpi = 320x320 px

When should we use dp or sp in layout?

Android recommends to use sp when you are setting font size and dp for everything else like width, length, height, margin, padding

Android sp vs dp texts - what would adjust the 'scale' and what is the philosophy of support

It is exposed in the settings menu on some Android devices (manufacturer dependent). It may also be altered by some accessibility options (device-dependent).

In general, you should always used scale-independent pixels, especially for a large body of text.

However if your text has to fit into a bounding-box of known size then you should use density independent pixels in order to ensure that the text always fits properly and that all characters are visible regardless of the users' setting.

In a nutshell: would increasing the text-size by around 5sp result in the text being unreadable or mangle your UI? If so use density-independent pixels. If not, use scale-independent pixels. However you should generally aim to use scale-independent pixels wherever possible, which means designing a UI that can accommodate different text sizes.

Android: Use sp not for text size, but element dimensions?

You can use sp for any sizes. But you have to be careful since sp can be scaled almost about twice the normal size and it might make your design ugly.

You can also use dp for your text sizes. It would mean that the texts will not be big enough for users that changed the font size in phone settings.

I think you should consult with your designers about this to come up with a solution.



Related Topics



Leave a reply



Submit