Saturday, February 11, 2012

How to use C-style printf format strings to include variables in strings.xml?


Variables can be included in the strings defined in strings.xml using the C-style printf '%' syntax. This is because getString passes the string through java.util.Formatter.
In the example above, getString substitutes %d for the integer 10 in the string message.
/ In strings.xml
<string name="unread_messages">You have %d unread messages</string>
// In Java code
String message = getString(R.string.unread_messages, 10);

No comments:

Post a Comment