You should add them to a string resource file and then reference them from your layout. This allows you to update every occurrence of the word "Yellow" in all layouts at the same time by just editing your strings. xml file. It is also extremely useful for supporting multiple languages as a separate strings.
String. xml file contains all the strings which will be used frequently in Android project. String. xml file present in the values folder which is sub folder of res folder in project structure.In Android Studio, we have many Views such as TextView,Button,EditText,CheckBox,RadioButton etc.
A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String. XML resource that provides a single string.
You don't need to set the "formatted" attribute for any of those items. When using quantity strings, there are only three possibilities:
%d
or whatever format you need%1$d
As for the getQuantityString
method, there are two overloads: one with only the resource id and the quantity, and one with an additional Object... formatArgs
parameter.
For case 1., you can use the getQuantityString(@PluralsRes int id, int quantity)
method.
For all other cases, i. e. if you have any parameters, you need the getQuantityString(@PluralsRes int id, int quantity, Object... formatArgs)
overload. Note: all parameters have to be present in the parameter array. That means, if the resource string displays the quantity, the quantity variable will be passed twice to the function.
That is because the quantity
parameter of the method itself is not considered when resolving the positional parameters of your resource string.
So if these are your resources,
<resources>
<plurals name="test0">
<item quantity="one">Test ok</item>
<item quantity="other">Tests ok</item>
</plurals>
<plurals name="test1">
<item quantity="one">%d test ok</item>
<item quantity="other">%d tests ok</item>
</plurals>
<plurals name="test2">
<item quantity="one">%2$s: %1$d test ok</item>
<item quantity="other">%2$s: %1$d tests ok</item>
</plurals>
<plurals name="test3">
<item quantity="one">%3$s: %1$d test out of %2$d ok</item>
<item quantity="other">%3$s: %1$d tests out of %2$d ok</item>
</plurals>
</resources>
then the appropriate calls to getQuantityString
are:
int success = 1;
int total = 10;
String group = "Group name";
getResources().getQuantityString(R.plurals.test0, success)
// Test ok
getResources().getQuantityString(R.plurals.test1, success, success)
// 1 test ok
getResources().getQuantityString(R.plurals.test2, success, success, group)
// Group name: 1 test ok
getResources().getQuantityString(R.plurals.test3, success, success, total, group)
// Group name: 1 test out of 10 ok
success = 5;
getResources().getQuantityString(R.plurals.test0, success)
// Tests ok
getResources().getQuantityString(R.plurals.test1, success, success)
// 5 tests ok
getResources().getQuantityString(R.plurals.test2, success, success, group)
// Group name: 5 tests ok
getResources().getQuantityString(R.plurals.test3, success, success, total, group)
// Group name: 5 tests out of 10 ok
quantity
parameterAs stated above, the key is to understand that the quantity
parameter of getQuantityString
is not used to replace the placeholders like %d
or %1$d
. Instead, it is used to determine the appropriate item
from the plurals
itself, in combination with the locale of the resource file.
Beware however that this is a less direct mapping than the attribute's name and its possible values (zero
, one
, two
, few
, many
, other
) might suggest. For example, providing an additional <item quantity="zero">
will not work (at least not in English), even if the value of the quantity
parameter is 0.
The reason is that the way plurals
work in Android is by the concept of quantity classes. A quantity class is a set of quantity values that have the same grammatical rules in a given language. This crucially means that
is dependent on the locale the respective resource file is for.
It is important to understand that both questions are decided only by grammatical necessity. Here are some examples:
other
is used, because in these languages sentences don't grammatically differ based on the given quantity.one
for the literal value 1, and other
for all other values including 0.one
, 2 is mapped to two
, 3-6 is few
, 7-10 is many
, 0 and 11+ is other
.one
(1, 101, 3001, ...). 2 and values ending in 02 are mapped to two
(2, 302, 1002, ...). 3, 4 and values ending in 03 or 04 are mapped to few
(3, 4, 6004, ...). Anything else is other
(0, 11, 48, 312, ...).many
(5, 12, 216, 4711, ...). Values ending in 2, 3 or 4 including 2-4 themselves are mapped to few
(3, 42, 103, 12035374, ...). This respects however that 12, 13 and 14 are exceptions from this rule because they are mapped to many
. (Side note: yes, grammatically speaking, 5 is many while 12035374 is few.)one
, because that's how their grammar works. You can see from this example that the quantity class one
doesn't even necessarily represent just one-ish numbers.As you can see, it can get fairly complicated to determine the correct quantity class. That's why getQuantityString
already does that for you, based on the quantity
parameter and the resource file's locale. The rules Android (mostly) plays by are defined in the Language Plural Rules of the Unicode Common Locale Data Repository. That is also where the names of the quantity classes come from.
All that means that the set of quantity classes needed to translate any quantity string can differ from language to language (Chinese just needs other
, English needs one
and other
, Irish needs all but zero
, etc.). Within one language however, all plurals
should each have the same number of items covering all quantity classes necessary for that particular language.
A call to getQuantityString
can be understood like this:
int success = 5;
int total = 10;
String group = "Group name";
getResources().getQuantityString(R.plurals.test3, success, success, total, group)
// \_____________/ \_____/ \___________________/
// | | |
// id: used to get the plurals resource | |
// quantity: used to determine the appropriate quantity class |
// formatArgs: used to positionally replace the placeholders %1, %2 and %3
The quantity
parameter's value of "5" will mean the used item
will be the one with the quantity class other
from Chinese, Korean, English, Slovenian and Armenian resource files, few
for Irish, and many
for Polish.
There are two special cases I'd also briefly mention:
Basically, the chosen class depends on language-specific rules again. It is neither universal how a class is chosen, nor guaranteed that any class required to cover all rules for integers is also used for any non-integers. Here are a few examples:
other
.few
.other
like in English.Note: This is how it should be according to the Language Plural Rules. Alas, Android has no readily available method for float
or double
at the moment.
If your display text has multiple quantities, e. g. %d match(es) found in %d file(s).
, split it into three separate resources:
%d match(es)
(plurals
item)%d file(s)
(plurals
item)%1$s found in %2$s.
(ordinary parameterized strings
item)You can then make the appropriate calls to getQuantityString
for 1 and 2, and then another one to getString
for the third, with the first two readily localized strings as formatArgs
.
The reason is to allow translators to switch the parameter order in the third resource, should the language require it. E.g., if the only valid syntax in a hypothetical language was In %d file(s) it found %d match(es).
, the translator could translate the plurals as usual, and then translate the third resource as In %2$s it found %1$s.
to account for the swapped order.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With