Grokking Android

Getting Down to the Nitty Gritty of Android Development

Wrapping Your Head Around Android’s Plurals

By 11 Comments

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>

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.

11 thoughts on “Wrapping Your Head Around Android’s Plurals”

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

    1. Wolfram Rittmeyer

      Nice one 🙂 Will be interesting to see what you are writing about this topic. Well, I have to wait for three more days.

  2. 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. 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.

  4. Morten Slott Hansen

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

  5. AndroidDeveloperLB

    Is this the best solution to show zero items, then:
    Also, which languages do support/need “zero” ?
    How come according to the website, the ranges for “few” and “many” have weird rules of mods ?

    1. Wolfram Rittmeyer

      Not necessarily. If you want to support few and many, this probably is no good solution. but for other languages like German or English it works pretty well.

      If you “need” zero or not (in English) is up to you. I prefer “No note selected” to “0 notes selected”. For other languages this might be different. And since I have too few knowledge of languages that make use of few and many to be able to say anything useful about that. Just be aware of the issues and discuss what you want to do with those making the business decisions and your translators.

      1. AndroidDeveloperLB

        When I wrote “which languages do support/need “zero” ” , I meant the “zero” value for the XML tag for plurals.

        1. Wolfram Rittmeyer

          Not many. See this page, I have linked to in my post: But Arabic might justify to care about it – depending on your target markets.

          1. AndroidDeveloperLB

            OK, to me it sounds a bit useless to use, when so few languages can use it.

Leave a Reply

Your email address will not be published. Required fields are marked *