55

I want to uses the plurals resource to produce a quoted number like "9".

In my plurals.xml:

<plurals name="posts">
  <item quantity="other">\"%dd\"<\item>
</plurals>

The code:

String text = res.getQuantityString(R.plurals.posts, meUser.postCount); 

When the postCount is 9, why does text turn out to be "%dd" and not "9"?

2 Answers 2

159

From the Android docs:

When using the getQuantityString() method, you need to pass the count twice if your string includes string formatting with a number. For example, for the string %d songs found, the first count parameter selects the appropriate plural string and the second count parameter is inserted into the %d placeholder. If your plural strings do not include string formatting, you don't need to pass the third parameter to getQuantityString.

ie res.getQuantityString(R.plurals.numberOfSongsAvailable, count, count);

1
  • Thanks a lot. For some strange reason, just adding the third parameter (same as second parameter) did solve my issue (although I have only one count in string) Commented Mar 3, 2020 at 6:18
1

Just in case anyone has a brain-fail like me today and still cannot get it working.

I had accidentally left my emulator in a non-English locale which meant my "one" translation was being ignored!

(Only the "other" entry was being used in my case)

Not the answer you're looking for? Browse other questions tagged or ask your own question.