I am Using placeholder %1$d in string.xml for int
value, and then I want to display %
sign in same TextView
so it's %
. So I've use it like this.
<string name="uploadProgress">%1$d%<string>
Now in Java Code I used it like this.
int progress = 10;
textView.setText(String.format(getString(R.string.uploadProgress), progress));
But it is showing:
java.util.UnknownFormatConversionException: Conversion:
I Tried with:
%
instead of %
%1$d
and %
(But I don't want space
actually.) But no luck. How can I achieve this? I've looked another SO questions for solution but I'm not able to find particular one.
In computer programming, a placeholder is a character, word, or string of characters that temporarily takes the place of the final data. For example, a programmer may know that she needs a certain number of values or variables, but doesn't yet know what to input.
put("text", "'${number}' is a placeholder."); result = NamedFormatter. format(TEMPLATE, params); assertThat(result). isEqualTo("Text: ['${number}' is a placeholder.] Number: [42] Text again: ['${number}' is a placeholder.]");
A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String. XML resource that provides a single string.
Substitute placeholder using the string replace method For the string replace method we can pass a regular expression to the first parameter and for the second one, we can use a function that return replace values.
I've run into this as well. There are two ways to escape a literal percent symbol in XML string resources.
For your case (a string with parameters, e.g. %1$d
) you should use %%
to escape the percent symbol literal.
If your string does not have parameters, then you should use \u0025
to escape the literal percent symbol.
If you use %%
in a string without parameters, then getString()
will resolve it as a literal %%
instead of escaping it. If you use \u0025
in a string with parameters, then getString()
will crash trying to treat that %
as a format parameter.
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