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:
px
Pixels - corresponds to actual pixels on the screen.in
Inches - based on the physical size of the screen.
1 Inch OR 2.54 centimetersmm
> Millimeters - based on the physical size of the screen.pt
> Points - 1/72 of an inch based on the physical size of the screen.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".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 whatsp
actually stands for, one doc says "scale-independent pixels", the other says "scaleable pixels".
From Understanding Density Independence In Android:
Density Bucket | Screen Density | Physical Size | Pixel Size |
---|---|---|---|
ldpi | 120 dpi | 0.5 x 0.5 in | 0.5 in * 120 dpi = 60x60 px |
mdpi | 160 dpi | 0.5 x 0.5 in | 0.5 in * 160 dpi = 80x80 px |
hdpi | 240 dpi | 0.5 x 0.5 in | 0.5 in * 240 dpi = 120x120 px |
xhdpi | 320 dpi | 0.5 x 0.5 in | 0.5 in * 320 dpi = 160x160 px |
xxhdpi | 480 dpi | 0.5 x 0.5 in | 0.5 in * 480 dpi = 240x240 px |
xxxhdpi | 640 dpi | 0.5 x 0.5 in | 0.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
How to Create Custom Spinner Like Border Around The Spinner with Down Triangle on The Right Side
Android - Multiple Custom Versions of The Same App
Supportmapfragment.Getmap() Returns Null
Android: How to Return Async JSONobject from Method Using Volley
Using Videoview for Streaming or Progressive-Download Video
How to Put Recyclerview Inside Nestedscrollview
Play Youtube HTML5 Embedded Video in Android Webview
Play-Services-Ads Conflicts with Appcompat
Android Input Connection Error
Android Collapsingtoolbarlayout Collapse Listener
Android Browser Bug? Div Overflow Scrolling
Finding Android Sdk on MAC and Adding to Path
Android - Expandable Textview with Animation
Gridlayoutmanager - How to Auto Fit Columns
Generating Edit Text Programmatically in Android
Using a Broadcast Intent/Broadcast Receiver to Send Messages from a Service to an Activity