How can I put a variable inside a resource string?

1

How can I put a variable within a resource string?

I have the following string:

<resources>
    <string name="card_closed">O cartão Nº _var_ está fechado! </string>
</resources>

How can I put my variable _var_ there programatically?

    
asked by anonymous 27.06.2016 / 16:21

1 answer

1

According to the answer in English ( here ) and the reference ( here ), put this code in the xml:

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

And so in the code:

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

EDIT:

Because you can have multiple variables inside a string.xml, they are numbered and typed. The% indicates the number of the variable, indicating the order in which it is to be filled programmatically. $ Sets the type of the variable. As exemplified in the links above, it can be $ s for string, or $ d for a number.

    
27.06.2016 / 16:24