Getting a String from Plurals to use in XML?

You have to set it by code:

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

You can have the run-time benefits of Plurals and the static benefit of string resources by leaving your string resources where they are and referring to them in your plural resources.

From https://developer.android.com/guide/topics/resources/string-resource.html#Plurals (emphasis added).

<item>

A plural or singular string. The value can be a reference to another string resource. Must be a child of a element. Beware that you must escape apostrophes and quotation marks. See Formatting and Styling, below, for information about to properly style and format your strings. Example:

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

<plurals name="cats">
    <item quantity="one">@string/cat</item>
    <item quantity="other">@string/cats</item>
</plurals>

Now you can use android:title="@string/cats" in XML files and also use

getQuantityString(R.plurals.cats, numberOfCats)

at runtime.


This can now be done in XML, using a data binding expression:

android:text="@{@plurals/cats(catCount)}"

See this page in the docs for more information: https://developer.android.com/topic/libraries/data-binding/expressions#resources