Grokking Android

Getting Down to the Nitty Gritty of Android Development

Wrapping Your Head Around Android’s Plurals

By

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:

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.

Wolfram Rittmeyer lives in Germany and has been developing with Java for many years.

He has been interested in Android for quite a while and has been blogging about all kind of topics around Android.

You can find him on Google+ and Twitter.