Android Plurals
A useful, but sadly rarely used string resource is plurals. Plurals are used for words or phrases which are spelled differently in the singular and the plural.
Plurals can be placed in any xml-file in the values directory, but usually, it will be placed in strings.xml. In Android, plurals are defined in a xml-file as:
1 2 3 4 5 6 7 | <?xml version="1.0" encoding="utf-8"?> <resources> <plurals name="numberOfCars"> <item quantity="one">%d car</item> <item quantity="other">%d cars</item> </plurals> </resources> |
To demolish the elements, the <plurals> element has a name attribute which equivalent to that of the <string> element. It is used to identify the words or phrases. Each <item> element has a quantity attribute which must be either one or other indicating singular or plurals, respectively.
To get the plurals from code, you can use the getQuantityString(int id, int quantity) function in the Resources class. In this example, we also pass an object as a format argument so that the %d will be replaced with the actual number of cars - just like you can do with the getString function:
1 2 3 4 5 | // Get the number of cars int count = getNumberOfCars(); Resources res = getResources(); // public String getQuantityString (int id, int quantity, Object... formatArgs) String cars = res.getQuantityString(R.plurals.numberOfCars, count, count); |
The main difference between the getting plurals and strings is that you have to pass a quantity as parameter. This parameter is used to get the correct string from the plural rules of the current language. In English, the item with the quantity attribute with the value of one will be returned if the quantity argument is 1 - otherwise the item with the quantity attribute with the value of other will be returned.
You can download a simple example here.
Read more about plurals at the official documentation.
Popular blog posts
-
Android Preferences
Preferences are an important part of an Android application. It is important to let the users have the choice to modify...
-
Incoming SMS Messages
During the development of version 1.3 of Kaloer Clock, I wanted to show an icon when a new sms message was received....
-
Permissions in Kaloer Clock
Android application security has recently become a focus of interest when talking about Android. A chinese developer...
-
Android Plurals
A useful, but sadly rarely used string resource is plurals. Plurals are used for words or phrases which are spelled...
Comments
Proper manner. Rich post. Thank you :)
Is this a feature for Android 2.2 (froyo) & above or can it be used in Android 2.1 ?
@GavinB
The R.plurals documentation page states that it has been implemented since API Level 1, which means that it can be used in all Android versions.
http://developer.android.com/reference/android/R.plurals.html
Don't use this thing, it's bugged with "zero" (that is never returned)
Wow didn't know this, thanks for this article! And see you at Droidcon UK hopefully :)
The above clearly states "singular or plural", meaning it's not designed to handle zero. For that, use:
No cars
My own issue is that I'm trying to use multiple substitutions in each item, and this is causing problems with the R.java file disappearing. Error messages say something about adding the formatted="false" attribute, but I've tried putting that in both the and tags, neither helps in the slightest. Any thoughts?
Sorry about the formatting of code samples in the previous post, this is what I meant:
<string name="none">No cars</string>
In reference to the formatted="false" attribute, I was trying to say I added it to both the <plurals> and <item> tags.
Hope this is clearer now.
@Craig
The problem is that the plurals strings are designed to make the singular/plural spelling easier (e.g. add -s in plural) - not to change the quantity word. Therefore, in English, the
<item quantity="zero">Zero cars</item>will not be chosen even though thegetQuantityString(R.plurals.numberOfCars, count, count);count argument is 0 as the spelling is equal to that of the plural form (an -s is added). In other languages, however, thequantity="zero"string might be chosen depending on the spelling.You can read more about the different quantity parameters on the official documentation.
Surprisingly well-written and infomartvie for a free online article.
Post new comment