Plurals Resource
Description
The resource plurals has a list of strings for expressing a numerical quantity in various ways.
For example:
- There is 1 student.
- There are 2 students.
- There are 20 students.
Android allows you to represent this variation as a plurals resource.
Example
The following example shows how you would represent these two variations based on quantity in a resource file.
<resources...>
<plurals name="my_text">
<item quantity="one">There is 1 student</item>
<item quantity="other">There are %d students</item>
</plurals>
</resources>
The first parameter to the getQuantityString()
method is the plurals resource ID.
The second parameter selects the string to be used.
When the value of the quantity is 1, you use the string as is.
When the value is not 1, you must supply a third parameter
whose value is to be placed where %d
is.
Resources res = your-activity.getResources();
String s1 = res.getQuantityString(R.plurals.my_text, 0,0);
String s2 = res.getQuantityString(R.plurals.my_text, 1,1);
String s3 = res.getQuantityString(R.plurals.my_text, 2,2);
String s4 = res.getQuantityString(R.plurals.my_text, 10,10);