28

So we know from many other posts that we should use sp rather than dp for text in Android, and we know the reason for this is to respect a 'user's preferences'.

But precisely what are these preferences? How might a user change this setting?

I cannot find any reference through the settings on my phone (I would have expected something in 'Accessibility' or 'Display'). So what is a user setting? Is it only done through the likes of an app such as 'Big Font'?

Assuming that it is (set by something like big font) - I have played with Google Docs and some other Google apps with the font set to 130%. While most layout stays fine, some gets a bit cut off and can't be read (and that is on a big screened SGS2). So, what is the approach to developing apps with text sizes using 'sp'? Do we make sure it works on 100% scaling and then ignore other settings - call it a special case that the user can worry about, or do we go out of our way to make sure things expand or are scrollable, in case the text overflows?

One argument is that we should use 'dp' to guarantee a user has a chance of seeing the text (even if they have to use a magnifying glass)

Thoughts/comments?

Sam
  • 3,453
  • 1
  • 33
  • 57

3 Answers3

62

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.

Joseph Earl
  • 23,351
  • 11
  • 76
  • 89
  • Thanks for this reply, and a nice rule of thumb. I wonder if i can set this quickly in the ADT layout viewer... Its not actually on ICS on my SGS2, so i guess manufacturers can still leave this out – Sam Jul 24 '12 at 21:18
  • Cheers for that info, will update my answer to reflect that this option is not all ICS devices. – Joseph Earl Jul 25 '12 at 08:35
  • 1
    This is the best explanation I've seen on when to use dp or sp. I've always heard that you should *always* use sp for text, but there really are scenarios when it doesn't make much sense. – Brian Aug 09 '13 at 22:03
  • 2
    If you need to fit your text into box of known size, then it's better to actually use `sp` for block size (instead of `dp` for text) – Dmitry Zaytsev Apr 30 '14 at 07:58
  • I just added an edit regarding that @DmitryZaitsev - the technique I have been using recently is to use `minHeight` on my `TextView` or box and then still use `sp` on the text itself. It should hopefully be a better general approach that deals with more situations (e.g. multiline text overflowing) – Sam Jul 03 '14 at 17:20
7

Using the sp unit is recommended for text because in ICS and above (could be Honeycomb too, correct me if I'm wrong), there is a preference for a user's font size. So, if you're using Gingerbread or lower, you won't be able to find this setting.

The preference is under Settings, Display, Font Size. There's also an option under Settings, Accessibility, Large text, too.

To address your question about how to go about using sp, note that by default, without changing any of the font size preferences, 1sp is equivalent to 1dp (also, they are equivalent before the preference was introduced). Like you've noted, designing for the case where a user has huge text would probably require you to assume things are going to need to scroll where you might otherwise not expect them to.

wsanville
  • 37,158
  • 8
  • 76
  • 101
  • actually i think i've seen the "sp" units a long time before honeycomb , so i think it was available before. – android developer Jul 24 '12 at 20:33
  • 1
    He's talking about the end-user text size preference setting being available in 3.0+, not the `sp` unit. That has been around since API 1. – Cat Jul 24 '12 at 20:34
  • As mentioned by Joseph Earl, this isn't on all devices (probably why I could not find it) – Sam Jul 24 '12 at 20:39
  • Right, the unit exists in lower versions, but I believe the preference was introduced later. – wsanville Jul 24 '12 at 20:39
1

The answer lies in looking at this particular issue holistically.

The motivation for using "sp" for font sizes lies in giving the developer power to control their layout in the face of user changing the font size on their device.

Example:

Lets look at 2 extreme cases:

1) User selects font size "small"

This is what my layout looks like:

http://postimg.org/image/kiyqeo2bh/

Here is the layout xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".MainActivity"
android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_row="0"
    android:layout_column="0"
    android:text="Material-Design ist die Scheiße"
    android:textSize="18sp"
    android:background="#ffff0000" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_row="0"
    android:layout_column="0"
    android:text="Material-Design ist die Scheiße"
    android:textSize="25sp"
    android:background="#ffff0000" />

2) If the user selects font size "huge":

This i what my layout looks like:

http://postimg.org/image/d7rax9wob/

My layout xml is same as above in case 1).

So, as you can see what happened here is the top TextView has sort of perfect font-size in sp because it does not wrap for the entirety of the range of font sizes (small to huge). But the bottom TextView completely messes up your layout/design in case 2).

So you as a developer can iterate and decide what size in sp works for your design and android will draw it for you.

dessertcook
  • 361
  • 3
  • 5
  • What if the screen were narrower? – Sam Aug 20 '15 at 13:08
  • Hi @Sam. We can define separate layout for narrower screens. You might have come across this page while muddling through heaps of docs :) - http://developer.android.com/training/multiscreen/screensizes.html. The above example is for a given size and points out the fact that "sp" is a unit independent of resolution. So lets say we want to display the word "Lizzy" on 2 devices of same size but different resolution - The resolution of one device is double the other. On the low-res device if Lizzy takes up 2 centimeters horizontally - it willl take up the same on the higher-res device also. – dessertcook Aug 28 '15 at 18:47
  • sorry, yes I've dug through all the docs a number of times - I was sort of prompting for more, as simply iterating SP alone in your example is not enough, you need to consider your screen size too (and perhaps think about localisation too!) – Sam Sep 08 '15 at 07:19
  • Both images are dead links – Jcorretjer Sep 18 '20 at 17:31