How to put variable inside string-resources?

JavaAndroid

Java Problem Overview


Is it possible to put variables inside string-resources? And if yes - how do i use them.

What i need is the following:

<string name="next_x_results">Next %X results</string>

and put an int in place of the %X.

Java Solutions


Solution 1 - Java

<string name="meatShootingMessage">You shot %1$d pounds of meat!</string>  


int numPoundsMeat = 123;
String strMeatFormat = getResources().getString(R.string.meatShootingMessage, numPoundsMeat);

Example taken from here

Solution 2 - Java

Just pass it throught getString() function as formatArgs Object.

int nextResultsSize = getNextResultsSize();
String strNextResultsSize = 
     getResources().getString(R.string.next_x_results, nextResultsSize);

XML:

<string name="next_x_results">Next %1$d results</string>

Solution 3 - Java

<string name="message">You shot %1$d pounds of meat! Put Second Variable String here %2$s and third variable integer here %3$d</string>
int intVariable1 = 123;
String stringVariable2 = "your String";
int intVariable3 = 456;
String strMeatFormat = getResources().getString(R.string.message, intVariable1, stringVariable2 , intVaribale3);

Solution 4 - Java

yes,you can use. after adding tag to string.xml file and then to retrrive the the same in your .java file you can follow this.

AndriodApp

String str = getResources().getString(R.string.name);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGuyFawkesView Question on Stackoverflow
Solution 1 - JavaYasin BahtiyarView Answer on Stackoverflow
Solution 2 - JavaRoelView Answer on Stackoverflow
Solution 3 - JavaAtul BhardwajView Answer on Stackoverflow
Solution 4 - Javakrupa parekhView Answer on Stackoverflow