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.

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.

Leave a Reply

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