Getting a String from Plurals to use in XML?

Duke picture Duke · Jul 4, 2013 · Viewed 22.6k times · Source

I am using Plurals to simplify my code. e.g., I used to have

<string name="cat">Cat</string>
<string name="cats">Cats</string>

Using Plurals instead of multiple strings, I now have

<plurals name="cats">
    <item quantity="one">Cat</item>
    <item quantity="other">Cats</item>
</plurals>

However, I used to retrieve strings to use as titles or summaries in my XML. e.g.,

android:title="@string/cats"

Having removed that string in favor of a Plural, I am now unsure how to retrieve my string from XML. I did make a naive attempt with

android:title="@plurals/cats"

but this just gives me @1234567890 instead of Cats (or Cat).

Anyone know if it is possible to retrieve a string out of a Plural from XML?

Answer

gwvatieri picture gwvatieri · Jul 4, 2013

You have to set it by code:

...setText(yourContext.getResources().getQuantityString(R.plurals.cats, catsCountVar));