android – strings.xml vs static constants-ThrowExceptions

Exception or error:

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
How to solve:

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.


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


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


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


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.


So my suggestion is

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


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

Leave a Reply

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