22

Here's what I'm thinking:

The strings on strings.xml should be use for the layouts (xml) to use. And static constants are for codes (.java) to use.

When it comes to best practices, I'd like to know which should be use.

If you have lots of strings, will it have performance effect?

getString(...) vs MyConstants.THIS_IS_A_CONSTANT
3
  • 2
    If you don't care about localisation or any of the other qualifiers that Android supports for resources, then you might as wel use Java constants. I would constants to be slightly faster, as they can be inlined by the compiler and do not need to be inflated. However, it's probably just a micro-optimization, so I wouldn't worry about it too much, provided you're not doing any looping operations on the UI thread in which you constantly inflate strings or string arrays. Commented Apr 23, 2013 at 3:03
  • If you want to publish your app in difference languages then all String that appear on the UI should be in the strings.xml. The rest you can make them static if you want. Commented Apr 23, 2013 at 3:09
  • 1
    If it's really "best practices" you're after, never hardcode UI strings, even in a small, personal project. Best to avoid bad habits that will one day cause your fellow programmers to pour salt into your coffee when they have to internationalize your code. Even in code, some strings should be resources, e.g. when processing user input which may vary by locale. Commented Apr 23, 2013 at 3:10

1 Answer 1

49

There are both some advantages and disadvantages ( I should say advantages and less advantages) in these two cases.

as in the comments of your question they said it all. I just want to add some minor points.

Localization:

For localization issue definitely String resource is the best as you can use different language file for differente Locale.

Memory:

As String resources are saved in xml file so there are some extra overhead (not a major one though)

Performance:

reading from memory is always faster than reading from file. Although in this case the performance difference is not significant

Maintainance:

It is just a personal opinion. To me maintaining res file is easier than maintaining string in class. string.xml is more readable to me.

Finally:

So my suggestion is

use string resources for the texts which will be displayed to user.

and

use static constants for internal puposes of your program like database names, internal variable, intent filter name etc.

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

4 Comments

As far as I know the xml files are also compiled in the apk so I don't think there is any overload.
Another thing is that a String from XML can be used directly in XML layouts, that's not the case for constants.
String.xml are compiled into java constants so there is no overhead of file reading.
@Talha Strings inside XML files are not compiled into java constants. You can validate it by decompiling apk file with apktool and check the XML files are still there

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.