Question

I'm just wondering what the benefits/overheads are for using @string rather than hard coding strings within the actual Java code... For Example:

// To get the string resource:
getActivity.setTitle(getString(R.string.my_string));

Is this the best practice for Things like Actionbar titles, Dynamically created button text, ect... Or should I just do this:

// Hardcoded string
getActivity.setTitle("My String");

I know there will be a bit more overhead doing it the first way.. Just not sure what best practice is.

Was it helpful?

Solution

Incase you were unaware as to the actual point of having the @string system please read over the localization documentation. It allows you to easily locate text in your app and later have it translated.

Edit Thanks to Hippo for clearing this up.

Using multiple strings of the same value no matter the method (Strings.xml vs programatically) doesn't seem to have any associated overhead. According to Oracle "All literal strings and string-valued constant expressions are interned" which means that the object is reused rather than re-created if you use it again.

OTHER TIPS

It is not good practice to hard code strings into your layout files/ code. You should add them to a string resource file and then reference them from your layout.

Reasons:

  • This allows you to update every occurrence of the same word in all layouts at the same time by just editing your strings.xml file.
  • It is also extremely useful for supporting multiple languages as a separate strings.xml file can be used for each supported language.

As the documentation says:

Suppose that your application's default language is English. Suppose also that you want to localize all the text in your application to French, and most of the text in your application (everything except the application's title) to Japanese. In this case, you could create three alternative strings.xml files, each stored in a locale-specific resource directory

I think these reasons are enough to recomend to go for @String

That way you have a fixed place to alter all your strings within the project. Lets say you used same string in 10 different locations in the code. What if you decide to alter it? Instead of searching for where all it has been used in the project you just change it once and changes are reflected everywhere in the project.

Well strings.xml would have to be parsed, wouldn't it? Then I suppose hard coded would be best for performance, though probably unnoticeable at runtime. People do choose to use it though in order to have all the strings in one spot in case there are plans to translate the app.

There are many benefits to setting the strings in a strings.xml file; in a nutshell, it allows you to use the same string in multiple locations, which is good if you somehow need to modify the string later. It also allows you to display the same text in different languages; hardcoding the string doesn't give you all those options.
BTW, you don't need to put every text in the strings.XML file; only the ones that might be used in multiple places in the application.
The general rule for placing strings in the strings.xml file are these:

  • Will it be used in multiple locations?
  • Will it be used in multiple languages?
  • Will it be dynamic or static?

I'm sure there are other reasons, but these are the ones I know.

Hopefully, this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top