I am trying to use getQuantityString method in Resources to retrieve Quantity Strings (Plurals) based on android developer guidelines Quantity String (Plurals)
The error I am getting is
Error:(604) Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?
Error:(604) Found tag </item> where </plurals> is expected
when I set up plurals as below
<plurals name="productCount">
<item quantity="one" formatted="true">%1$d of %2$d product</item>
<item quantity="other" formatted="true">%1$d of %2$d products</item>
</plurals>
and trying to read it as below
productIndexCountText.setText(getResources().getQuantityString(R.plurals.productCount, position, size));
One workaround is to break the string up to use plural only for the last part of the string and concatenate the two parts. But I am trying to avoid doing that if possible.
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 ressource id and the quantity, and one with an additionalObject... 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 param array. That means, if the resource string displays the quantity, the quantity variable will be passed twice to the function.So if these are your resources
then the appropiate calls to
getQuantityString
are: