Android lookup string resource

Android-lookup-string-resource-featured.png

Android SDK Quick Tip: Formatting Resource Strings

This quick tip demonstrates how to produce and employ string assets as format strings. You’ll accomplish this by creating and setting up the right string assets in XML and taking advantage of them programmatically to provide the different typed parameters.
Step : Getting Began

Start by creating an Android project. If you want to just follow along, you will find a sample project with code for formatting strings.
Step One: Allowing the String Assets

String assets are frequently saved inside the /res/values/strings.xml file from the resource hierarchy. Any strings you increase the strings.xml file is going to be accessible in your application. This is an example strings.xml file:
view plaincopy to clipboardprint?



Format Strings
Years old!

This string resource file defines two strings. One for that application title and the other known as hello. Keep in mind that in case your string resource has complex character codes, you may want to quote the whole string. You can include format string assets almost as much ast you need to do regular string assets.
Step Two: Why Would You Use Format Strings?

Format strings really are a convenient way to develop one string which has variable content. This really is best highlighted by example. In homage towards the popular gaming Or Trail, let us take a look at an infamous message gamers inevitably saw while dragging their loved ones over the Area:

&ldquoYou shot <> pounds of meat!&rdquo

The amount of pounds of game meat changes based on the way the player shoots. You will find various ways (both negative and positive) that designers might approach this issue. A developer might:

Steer clear of the problem entirely and make up a generic string, shying from the specifics: &ldquoYou shot something!&rdquo
Create two string assets (e.g. &ldquoYou shot &rdquo and &rdquo pounds of meat!&rdquo) and sandwich them together around several programmatically.
Brew their very own solution, doing a bit of black miracle with string search and replace techniques (e.g. &ldquoYou shot ##PUT_NUM## pounds of meat!&rdquo and also the String replace() method).
Request themselves if format strings operate in Android resource files and browse this tutorial.

We did not make these approaches up. We have seen all of them. Couple of of those approaches are reasonable: the foremost and the final.

Sometimes, approaching having a generic string may be the right answer&mdashit’s easy and straightforward. Generally, this is actually the ultimate way once the information being relayed can be created generic with no application losing something. In other cases, you have to communicate important and particular information towards the user. In Or Trail, should you shot 2 pounds of meat, then you’d keep hunting (or starve), whereas, should you shot 1234 pounds of game, your wagon was at their maximum anyway, so you’d likely decide to mosey along lower the trail (having a full belly).

You may request why the sandwich approach is not so excellent. First, using two strings clutters your resource files and means they are hard to maintain. Next, when you attend internationalize the application, you might find that individuals two strings are no more appropriate&mdashit could possibly get very complex in case your string has several parameter, too. Brewing your personal solution means presenting more code to keep and presuming that can be done string manipulation much better than the stock Java libraries can.

Easier to make use of the standard techniques for formatting strings and make up a single string resource with parameters.
Step Three: Creating Format Strings

Format strings might have one parameter or many. Each parameter is designated and typed. The parameter number is specified using % then the amount of the parameter, which matches an order where the parameter values is going to be provided programmatically. For instance, the very first parameter could be %1, the 2nd %2, etc. The 2nd item each parameter has is really a type (think C-style printf()), per a $ along with a letter indicating that type.

For instance, a string could be $s several may be $d. Therefore, our string resource might be crafted as:
view plaincopy to clipboardprint?

You shot %1$d pounds of meat!

Step Four: Delivering Format Strings with Number Parameters

Since you have produced an effective format string, you are able to load it, format it, and display it to customers. The next code loads the string resource, increases the single number parameter and creates the finished string:
view plaincopy to clipboardprint?

int numPoundsMeat = 123
String strMeatFormat = getResources().getString(R.string.meatShootingMessage)
String strMeatMsg = String.format(strMeatFormat, numPoundsMeat)

Step Five: Delivering Format Strings with Multiple Parameters

Let us try another example. This time around we’ll create two more format string assets, each with two parameters. In Or Trail, the player’s family frequently got sick and often died. The 2 messages gamers frequently saw went something similar to this:

&ldquo<> has << name of disease or trauma>>!&rdquo
and
&ldquo<< name of disease or trauma>> has wiped out <>!&rdquo

Allowing the two format string assets is rather straightforward:
view plaincopy to clipboardprint?

%1$s has %2$s!
%1$s has wiped out %2$s!

The next code loads the string assets, increases the parameters, and creates the finished strings:
view plaincopy to clipboardprint?

String title = “Sally”
String disease = “Typhoid”
String strDisease1Format = getResources().getString(R.string.diseaseMessage)
String strDisease1Msg = String.format(strDisease1Format, title, disease)
String strDisease2Format = getResources().getString(R.string.diseaseMessage2)
String strDisease2Msg = String.format(strDisease2Format, disease, title)

Android SDK Format String Output
Bonus: Format String Parameter Order is Flexible and Locale-Friendly

You might have observed within the last step the parameters were swapped within the second string (strDisease2Msg). This can be a little annoying, no? Fortunately, there’s simply no reason format string contents must dictate an order from the parameters. Rather, simply number the parameters within the format string resource within the order that you would like to provide them. For instance, should you always wish to give you the character title, then the condition title, then define your format string such as this:
view plaincopy to clipboardprint?

%2$s has wiped out %1$s!

Therefore, your code would take a look such as this:

String strDisease2Msg = String.format(strDisease2Format, title, disease)

That causes it to be much simpler around the developer. Also, when it comes to string translation, most of the string parameters get jumbled in different languages anyway.
Conclusion

Within this quick tip you learned using format strings as Android string assets. Format strings permit flexible and maintainable application assets.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>