31

When I use

android:textSizes="20dp" 

in my XML for a textView, I got a warning "Should use "sp" instead of "dp" for text sizes."

Why should "dp" not be used? What is the correct approach? How can I achieve same textsizes on different displays?

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Peter Panne
  • 475
  • 1
  • 5
  • 15
  • 3
    _An sp is the same base unit (as dp), but is scaled by the user's preferred text size (it’s a scale-independent pixel), so you should use this measurement unit when defining text size (but never for layout sizes)_. [Supporting Different Densities](http://developer.android.com/training/multiscreen/screendensities.html). – Onik Jun 01 '14 at 15:21
  • 1
    I know the difference but I don't get why I can't use dp for font sizes. I mean I wanna have same text sizes on different screens otherwise fonts get very small on large Displays. How can I achieve that text appears in same sizes then? – Peter Panne Jun 01 '14 at 15:26
  • @Rotwang what about the warning "Avoid using sizes smaller than 12 sp:10sp" – pukingminion Jul 19 '17 at 18:02
  • 3
    I'm surprised no one has mentioned that sp might cause inconsistent ui design unless you're code it very strict, e.g. height of box can't fixed in dp since its text view is in sp. – 林果皞 Aug 29 '17 at 10:55
  • 1
    who knows a fast way to convert all textSize dp to sp with AS? – Dennis Anderson Oct 23 '17 at 14:50
  • I agree 100%. SP can mess up your layout pretty bad if a user chooses bigger font size. I'd rather have all the content displayed than have a bigger font but lots of truncation... – joe1806772 Aug 13 '20 at 10:58

3 Answers3

47

You should always use SP for fonts as it respects the user preferences. Here is an example Lets understand it with the help of an example -

Text with SP and DP

enter image description here

Change the device text setting (Settings -> Display -> Font Size)

enter image description here

Now reopen the app and relook at the texts, You will see that the text which was using SP has different height than DP.

enter image description here

Ajit Singh
  • 2,436
  • 23
  • 27
  • 11
    I downvoted because of 'you should always'. For instance, the UX team enforces devs to use fixed text size for some layouts (tablets most notably). The example would be 'just double the size of font for tablets'. In this case dp metrics works perfectly. – Michał Dobi Dobrzański Jul 31 '18 at 06:41
  • 1
    @MichałDobiDobrzański What you say is correct, if we don't want the font size to be effected by what the user sets in the settings, we can go for dp, but then again we are restricting the user, (just like giving them a music file, and restricting them that you should play this only at a certain volume even if they feel that the file is best played at a certain volume) – Sreekanth Karumanaghat Nov 26 '19 at 08:02
  • There are clear and obvious times you *should* use DP for text. One way to look at it is that with *larger* text - things like headlines - it's reasonable that it is fixed as part of the design and cannot be changed. For example, there's some sort of splash screen with the client logo and some sort of tagline or marketing headline in large text (think of something like a book cover). It's really natural and OK that that is fixed size, DP. It's more like "a design layout". On the other hand for any "reading" text the user has to read, it's great to make it SP. – Fattie Mar 04 '21 at 12:32
28

You can use sp and dp. As you know in Android settings you can change text size (Settings -> My device -> Display -> Font size). All your textView in sp would change after changing font size in settings, dp - would not change

resource8218
  • 1,445
  • 1
  • 20
  • 33
0

As @GiruBhai shared,it is more convenient to use sp instead of dp for the text size since it can be changed -unlike dp - according to the user's preferences.Which may be fulfilling your users needs better.

More info. : Dimensions in Android

Source : developer.android.com

Recomer
  • 178
  • 2
  • 12