22

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.

4 Answers 4

28

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Strings are interned, so I don't think your second comment applies.
@Hippo That's the point I was trying to make there. If the String "Title" was created in one class and in five other classes. Using Strings.xml will refer to the one instance. Rather than in those 5 classes instantiating a new String object. Does that sound accurate?
Also, later on, you don't have to wade through code files just to make some cosmetic changes and correct spelling mistakes.
@KDEx: I don't think it does. Once a particular String literal is instantiated, it's added to the String pool. Subsequent uses of the same String literal will reference the previously instantiated String.
@Hippo thanks for helping to clarify. I made an edit to reflect that.
|
4

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.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.