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?
android:text="@string/hello" /> String string = getString (R. string. hello); You can use either getString(int) or getText(int) to retrieve a string.
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.
In the Project > Android panel on the left, select ModuleName > res > values. Double-click strings. xml to open it for editing.
You have to set it by code:
...setText(yourContext.getResources().getQuantityString(R.plurals.cats, catsCountVar));
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