Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a String from Plurals to use in XML?

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?

like image 402
Duke Avatar asked Jul 03 '13 22:07

Duke


People also ask

How do I convert a string to a string in xml?

android:text="@string/hello" /> String string = getString (R. string. hello); You can use either getString(int) or getText(int) to retrieve a string.

Where can I find strings in xml?

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.

How do I open a string in xml?

In the Project > Android panel on the left, select ModuleName > res > values. Double-click strings. xml to open it for editing.


Video Answer


1 Answers

You have to set it by code:

...setText(yourContext.getResources().getQuantityString(R.plurals.cats, catsCountVar)); 
like image 93
gwvatieri Avatar answered Sep 24 '22 01:09

gwvatieri