Reputation: 2365
I have android app and this string in resources:
<string name="create_group_select_people">Select up to %1$d people!</string>
This is called from fragment:
Integer countMax = 5; //also tried just "int" - nothing changed
getResources().getString(R.string.create_group_select_people, countMax);
but I got error:
Format string 'create_group_select_people' is not a valid format string so it should not be passed to String.format
I can't understand what is wrong? When I launch app - it shows me literally "Select up to %1$d people!"
Upvotes: 55
Views: 39713
Reputation: 15113
Set parameter formatted to true in resources:
<string name="some_text" formatted="true">
Use for String.format method. Parameter one: %s1
</string>
and use it this way:
String.format(context.getString(R.string.some_text,"value 1"))
or this way:
context.getString(R.string.some_text,"value 1")
Note: the formatted flag should be set to true only for strings with placeholders.
Upvotes: 23
Reputation: 63
Check if this string is mentioned in other xml-files of the strings folder, this might help.
Upvotes: 0
Reputation: 146
Still buggy with Android 2021.2.1 Patch 1 Anyway project builds and runs properly, nothing really changed for me after a clean, rebuild, invalidating caches.
Managed to remove the warning like this
String msg = String.format(String.valueOf(R.string.msg_token_fmt), token);
Upvotes: 0
Reputation: 41
Just add
<?xml version="1.0" encoding="utf-8"?>
as first line to your strings.xml
Upvotes: 3
Reputation: 653
If it shows the red color error, then add strings value like this and it will work.
If you want the string format:
<string name="msg_token_fmt" formatted="true"> %s
</string>
If you want the float format float:
<string name="msg_token_fmt" formatted="true"> %f
</string>
If you want the number format:
<string name="msg_token_fmt" formatted="true"> %d
</string>
Upvotes: 2
Reputation: 91
I had the same issue, but it has a different origin. My app has some languages and in one translation there was no "%" but a "$" sign instead. It still worked, except in this specific language and I received this hint at the respective line were I used the string.
Upvotes: 0
Reputation: 2257
In my case invalidating cache with restart did not help, but having consistent formatting parameters (e.g. %s
) across other the locales resolved the issue. I hope this might be helpful to someone in the future.
Upvotes: 0
Reputation: 571
This error will be shown if you have the same string in multiple string files (translations), but one of them doesn't have proper format like missing "%s" or "%1$s", which will be used to place paramters passed (ex: "countMax") in below line.
getResources().getString(R.string.create_group_select_people, countMax)
So, please check that before you are going to try any other answers mentioned above.
Upvotes: 25
Reputation: 22867
For the formatting of the string to be correct:
String.format(Locale.ENGLISH, itemView.context.getString(R.string.XXX), "%17")
Also the special characters do not have to interfere with the injection of parameters. For that, take into account the exhaust outlets for special characters
From: <string name="XXX">% %1$s</string>
To: <string name="XXX">%1$s</string>
Finally XXX is "%17"
Good Luck.
Upvotes: 0
Reputation: 1255
For the sake of others who might find this thread, one possible cause of this warning is that you have multiple languages defined in string resource files and you did not specify format arguments in one or more of them.
For example, if you have a strings.xml in your values folder, and another strings.xml in your values-es folder, but you only added format arguments to the strings.xml in your values folder, then the warning will be triggered because of the lack of format arguments in the string resource of strings.xml in your values-es folder.
Upvotes: 10
Reputation: 77
Make a function and put your code inside it and add this @SuppressLint("StringFormatInvalid")
just before the function. Hope it helps.
Upvotes: 0
Reputation: 6269
Try File -> Invalidate Caches / Restart...
, it fixed the problem for me.
Upvotes: 14
Reputation: 427
Try doing a 'clean project' followed by a close and reopen of Android Studio.
That fixed it for me, it looks like some minor Android Studio /Lint bug.
Upvotes: 1
Reputation: 781
I just copied the code and it works well. so you may need to check some other place,Here are my suggestions.
Upvotes: 46
Reputation: 27
You're probably missing the format method of the String class, If you're setting it in a TextView, the proper way is:
textView.setText(String.format(getResources().getString(R.string.create_group_select_people), countMax));
Upvotes: 0
Reputation: 1696
You need String formatter. Please change below code from
getResources().getString(R.string.create_group_select_people, countMax);
to
String temp = String.format(getResources().getString(R.string.create_group_select_people), countMax);
For more detail information refer
Upvotes: 1