There are many tutorials on the web about String formatting on Android. Unfortunately, most of them are about the usual String.format method. In this post, I’ll show you the different ways to display your message.

Most of the labels or messages you use in your apps need to have some parameters. Lets say you want to display the current city to the user with a sentence like you are in Barcelona. The city name is of course provided by a magic method (magic because that subject will not be covered in this post). The rest of the sentence is in the string.xml resource file. Of course, as a clever Java developer, you’ll use the String formatter. Using Java, the usual way is

String label = String.format("You are in %1$s", "Barcelona");

So, as your String pattern is in your resource file like this:

<resources>
    <string name="location_in">You are in %1%s</string>
</resource>

As you get the city with a method getCityBySomeMagic(), you are tempted to use the following:

String label = String.format(getResources().getString(R.string.location_in), getCityBySomeMagic());

And this is the way it is documented in the Android documentation.

At this point, let me just remind you one hint. The String.format() method will ignore all the extra arguments. This is useful since sometimes, you cannot locate the user. So you have another string to display:

<resources>
    <string name="location_unknown">You are lost</string>
    <string name="location_in">You are in %1%s</string>
</resource>

As this String takes no argument, you may want to write something like:

if ("".equals(getCityBySomeMagic()) {
    String label = String.format(getResources().getString(R.string.location_unknown);
} else {
    String label = String.format(getResources().getString(R.string.location_in), getCityBySomeMagic());
}

Of course, we’ll assume that the getCityBySomeMagic() does return a city name or an empty String. But as the extra arguments are ignored, you can get ride of 5 lines and end up with just:

String label = String.format(getResources().getString("".equals(getCityBySomeMagic()) ? R.string.location_unknown : R.string.location_in), getCityBySomeMagic());

I will not argue about the best practice, either you prefer to have a clean structured if condition or an less readable inline if. Be just aware that you can write your code both ways.

I don’t know why the Android developer documentation focus about the String.format method, but you write the above code in an even cleaner way. It is less known that the Ressources.getString() can also accept arguments. So, you simply don’t have to call the String.format method. Just add the arguments in the getString method as follow (with another way of writing the code):

int locationString = R.string.location_unknown;

if (!"".equals(getCityBySomeMagic()) {
    locationString = R.string.location_in;
}

String label = getResources().getString(locationString, getCityBySomeMagic());

From a personal point of view, I found this last example more readable. I haven’t checked which of those writings are the more efficient, but if it has to come to code maintenance, I do prefer the last one.

Feel free to share you opinion in the comments.

About Darko Stankovski

Darko Stankovski is the founder and editor of Dad 3.0. You can find more about him trough the following links.