4

What's the point of defining strings in xml at the res/values/string.xml directory? Is it more efficient than just defining the strings as constants within your classes? (e.g. Database table creation scripts, etc..)

Is it a matter of organization or is there some benefit in how Android handles these objects in memory?

3 Answers 3

9
  • So you can easily translate them into different languages.
  • So they're nicely organized and you have them all in one place.
Sign up to request clarification or add additional context in comments.

1 Comment

It's really nice since it's totally automatic. You provide strings in different languages, and Context.getString() will return whichever language is the best fit. You can still format it with arguments (%1$s, %2$d), and you can even change the order in different languages.
2

You can easily translate your app if you use strings.xml. Just create a new folder with suffix like values-cs and put the xml with czech strings in it and the whole app will be translated to czech if you have set czech localization in your phone.

Comments

1
  • applications access them/compute them faster than normal strings
  • localization

The hello string is defined in the res/values/strings.xml file. This is the recommended practice for inserting strings to your application, because it makes the localization of your application to other languages graceful, without need to hard-code changes to the layout file.

  • language (computer) translation

For me the first point is the deal breaker, anything to make your app faster. (this is assumed from the countless hours of creating my own applications and being told using string.xml is best from an optimization point of view, plus, especially in a long listview, it does seem to load faster for me (droid A855) )

2 Comments

If it was true, sure. But a string literal is basically just loading a fixed address. Calling Context.getString() will call a native function that needs to look up the actual String object using a hash lookup, not to mention that it has to determine the proper language to retrieve it from.
That said, retrieving a string is not what makes your app faster or slower, unless you have a loop where you load 1000000 strings for whatever reason. Don't optimize in the wrong place.

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.