50

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!"

Wackaloon
  • 2,285
  • 1
  • 16
  • 33

16 Answers16

45

I just copied the code and it works well. so you may need to check some other place,Here are my suggestions.

  1. clean project
  2. check multi-language files
  3. or just use String.format just like others said
Vojtech Ruzicka
  • 16,384
  • 15
  • 63
  • 66
ohdroid
  • 771
  • 6
  • 3
  • 1
    Clean build worked for me. Why does this work do you know? What went wrong in the first instance. – COYG Aug 18 '18 at 10:26
  • Number 2 did it for me. At first I thought there was a problem with lint because I was seeing the same warning three times, but it turns out it was one for each of the languages were the string wasn't formatted correctly – Nicolás Carrasco-Stevenson Dec 19 '18 at 10:26
  • 2
    I'm not sure about others but for me, there is an issue with the percentage(%) symbol may be it is related to Unicode. There should be a small symbol (https://www.compart.com/en/unicode/U+FE6A) not the big symbol(https://www.compart.com/en/unicode/U+0025). Hope this helps :) – Chitrang Jun 25 '19 at 20:06
  • For me I needed to check option 2 : **check multi-language files** – botanga May 16 '20 at 12:57
  • If you have a percent sign you need to escape it as `%%`. You don't need to use an unusual unicode variant of percent as @Chitrang suggests, and if you do you may find it is missing from some fonts. – alex bird Aug 24 '20 at 17:26
22

Set parameter formatted to true in resources:

<string name="some_text" formatted="true">
    Use for String.format method. Parameter one: %s1
</string>

and use this way:

String.format(context.getString(R.string.some_text,"value 1"))

or this way:

context.getString(R.string.some_text,"value 1"))

Note:formatted flag should be set to true only for strings with placeholders

MiguelSlv
  • 14,067
  • 15
  • 102
  • 169
  • This did it for me. For some reason, I needed to **restart Android Studio** to remove the error message. – User Feb 26 '20 at 17:24
21

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.

sHaRkBoY
  • 481
  • 4
  • 8
14

Try File -> Invalidate Caches / Restart..., it fixed the problem for me.

Bernd Kampl
  • 4,837
  • 4
  • 21
  • 26
9

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.

jwitt98
  • 1,194
  • 1
  • 16
  • 30
2

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>
Kirby
  • 15,127
  • 10
  • 89
  • 104
btm me
  • 368
  • 3
  • 11
1

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

Shadow Droid
  • 1,696
  • 1
  • 12
  • 26
1

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.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Venu G.
  • 427
  • 1
  • 5
  • 10
1

Just add <?xml version="1.0" encoding="utf-8"?> as first line to your strings.xml

Alex Drozd
  • 21
  • 3
0

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));
Julian
  • 27
  • 5
0

Make a function and put your code inside it and add this @SuppressLint("StringFormatInvalid") just before the function. Hope it helps.

Shivam Maindola
  • 67
  • 2
  • 10
0

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.

Source

Braian Coronel
  • 22,105
  • 4
  • 57
  • 62
0

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.

ddsultan
  • 2,027
  • 1
  • 19
  • 19
0

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.

0

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);

0

Check if this string is mentioned in other xml-files of the strings folder, this might help.

Iurii Mg
  • 63
  • 7
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 02 '22 at 06:46