I'm wondering why my TextView size is different for different devices even if I set the text size in sp or dp.
Please help me. Thanks.
When setting text sizes, you should normally use sp , or “scale-independent pixels”. This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.
You should think of this as the normal font size, and basically everything else a variation on it. For instance, while 14sp is the default text size when the text can be quite long, when there's only a small modal with a bit of text, it's 16sp! Notice that it's a bit lighter to make up for this size boost.
Open your device's Settings app. Tap Accessibility, then tap Display size. Use the slider to choose your display size.
This post explains the difference:
dip/dp (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 (scale independent pixels):
like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.
And this post explains why dp
and sp
values may differ:
The dp has constant ratio transition to pixels:
dp = px * ratio
. Ratio will never change on any particular device.The sp has scalable ratio:
sp = px * ratio * scale
. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With