I have two string resources. The first one in main\values is in English and doesn't have a string argument:
<string name="example">Have a good day!</string>
But in French, under values-fr, we have a different phrase with one argument, like so:
<string name="example">Bonne journée M. %1$s!</string>
How should I use the resource string formatter? This works, but it doesn't seem right (and it generates a lint warning):
textView.setText(getString(R.string.example, name));
The lint warning is "StringFormatInvalid" and the description is "Format string 'example
' is not a valid format string so it should not be passed to String.format
"
There is nothing wrong with your approach. The formatter works perfectly even if there are too many arguments. The leftover arguments just get dropped.
Lint warnings are there to indicate possible unintended errors in the code. But if you know what you are doing you just may suppress the warning by adding the @SuppressLint("StringFormatMatches")
annotation to the enclosing method or class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With