How do I place a variable inside a Resources string?

Asked

Viewed 405 times

1

How can I place a variable inside a Resources 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 there _var_ programmatically?

1 answer

1


According to the reply in English (here) and the reference (here), put this code in 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:

Since one can have several variables within a.xml string, they are numbered and typed. A % indicates the variable number, indicating the order in which it will be filled in programmatically. The $ defines the variable type. As exemplified in the links above, it can be $s for string, or $d for a number.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.