String formatting in strings.xml

suggest change

Defining Strings in the strings.xml file also allows for string formatting. The only caveat is that the String will need to be dealt with in code like below, versus simply attaching it to a layout.

<string name="welcome_trainer">Hello Pokémon Trainer, %1$s! You have caught %2$d Pokémon.</string>
String welcomePokemonTrainerText = getString(R.string.welcome_trainer, trainerName, pokemonCount);

In the example above, %1$s uses $ between the parameter number and the type. s denotes a string, and d denotes an integer. trainerName and pokemonCount are not declared here.

Note that getString() is a method of Context or Resources, i.e. you can use it directly within an Activity instance, or else you may use getActivity().getString() or getContext().getString() respectively.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents