Wrapping Your Head Around Android’s Plurals

Android has this nice feature to make your handling of strings with numbers easy. If you want to display slightly different messages that depend on how many items a user has selected, you can use Android’s plurals element in your strings.xml:

<plurals name="number">
   <item quantity="one">One item selected</item>
   <item quantity="other">%d items selected</item>
</plurals>

Of course you have to use this string somewhere in your code and you have to pass in the number – otherwise Android can’t help you. This also is no magic:

numberStr = context.getResources().getQuantityString(
      R.plurals.number, count, count);

The first argument obviously refers to the above plurals element of the strings.xml file. The second defined which plural to use. And the third which value to use for substitution – that is with which value to replace the "%d" formatting code.

You shouldn’t use the overloaded method with two arguments only as the formatting code in your xml file wouldn’t be substituted.

For an introduction to plurals see also Anders Kalør’s post about plurals in Android.

Possible plural quantities

Of course you cannot use any arbitrary number for the quantity attribute. The possible quantifiers are limited to these:

  • zero
  • one
  • two
  • few
  • many
  • other

But not all languages support all values. English for example only knows about "one" and "other".

Other languages like Arabic, Irish or the Slavic languages use either all or at least some more of these. You can find a full list of which selections work in which language on the Unicode consortium’s page.

The problem with plurals

Now I wouldn’t blog about it if all were nice and dandy. Well, okay, I would. But, alas, it is not.

If your Locale doesn’t support a quantity value according to the rules linked above, Android will ignore this selection.

Try to use a plural for zero. Does it work? Most probably not, because in most languages "zero" deserves no special treatment. Only five languages have support for it.

If the quantity value is not supported for a language, Android uses the “other” selection – in compliance with above rules.

But what if you want to display a different text for zero elements? This definitely isn’t uncommon. Wouldn’t you prefer to see a text like “No element selected” to the text “0 elements selected”? Well, I would. It is much nicer to read. But your are out of luck here.

It get’s even worse if you consider ordinals: 1st, 2nd, 3rd, 4th and so on. How to deal with them? Well, not with Androids plurals element – that’s for sure. But, to be honest, I think ordinals should not pose a problem too often – even though this question has popped up on StackOverflow.

I have posted a feature request to Android’s issue tracker, so that you could force Android to use a selection even if it’s not part of the Unicode rules. Please vote for this issue by starring it!

Has this issue with plurals ever posed a problem to you? And how did you deal with it? Please let us know in the comments.

Share this article:

You can leave a response, or trackback from your own site.

6 Responses to “Wrapping Your Head Around Android’s Plurals”

  1. Mark Allison says:

    Great minds think alike! Guess what the subject of this Friday’s Styling Android post is?

  2. Tom anMoney says:

    While “zero”, “two”, “few”, “many” were always supported by the XML schema, they weren’t actually used until Honeycomb. So, if your app needs to work on Android < 3.0, and have the need for one of those options, the plurals mechanism cannot be used at all. In my app, which supports Russian and Hungarian, I rolled by own plurals implementation.

  3. […] Since this article was written, Wolfram Rittmeyer has published an article on plurals on Grokking Andorid which contains some information on some of the pitfalls with using […]

  4. Alec G says:

    Another drawback; “The resulting string will be stripped of any styled text information.” I had some bold letters in my string and the bold was stripped by getQuantityString.

  5. Don’t forget the whole issue with gender translation – this is something that is killing me atm. :(

Leave a Reply

You can also subscribe without commenting.

Subscribe to RSS Feed My G+-Profile Follow me on Twitter!