Using C-style printf format strings to include variables in strings.xml

2 votes · 0 comments

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.

This tip was taken from Bram Borggreve's blog ( bVirtual.nl ).

raw ·
copy
· download
// In strings.xml <string name="unread_messages">You have %d unread messages</string> // In Java code String message = getString(R.string.unread_messages, 10);

Be the first to comment

Sign in with OpenID