There are the many scenarios where we format a statement at the run time, means we attached a string, decimal, floating point number into a string of resource file.
If we need to format our strings, then we can do so by putting our format arguments in the string resource, as demonstrated by the following example resource.
1 |
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string> |
In this example, the format string has two arguments: %1$s
is a string and is a%2$d
decimal number.
We format string by calling-
1 2 |
Resources res = getResources(); String text = res.getString(R.string.welcome_messages, username, mailCount); |
Or, We also format a string resource as-
1 |
<string name="welcome_messages" formatted="false">Hello, %s! You have %d new messages.</string> |
We format string it –
1 |
textView.setText(String.format(getString(R.string.welcome_messages), userName, 2)); |
References:- https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling