Where to store string values? In strings.xml or in a constants class?
In a nutshell:
meaning to use in code: always use constants class.Advantage: codes remain integrated and your package can be used in other projects / contexts. You cannot do this with string.xml as it does not carry over with your package.
value to display in UI: use string.xml. Advantage: You can use localization to display translated texts.
Some situation can arise when both options appear viable. Then you will need to decide where its associated values โโare stored.
source to share
Generally use strings.xml
because android uses this XML to enable translating your application to different languages, which it cannot do with hardcoded strings.
The official Android localization guide says this:
Move all strings to strings.xml
As you build your applications, remember that you don't need to hardcode any string. Instead of declaring all your strings as resources in your strings.xml file by default, making it easy to update and localize. The strings in the strings.xml file can be extracted, translated, and integrated back into your application. (with appropriate qualifiers) without any changes to the compiled code.
If you are creating images with text, put these strings in strings.xml as well and regenerate images after translation.
Strings that will not be displayed to the user in any way should not be stored in XML because they will never need to be translated and you probably don't want the android system to mangle them the way you might at runtime I do not know.
source to share
If the strings are user-readable text that could potentially be translated into other languages โโ(names of buttons, labels, notification / error messages, etc.), then they should be in the strings.xml file (in fact, this can be any filename you like, not just "strings"). If the string is a constant that is used internally by the application (package / intent keys, fragment tags, etc.), they must be declared in the class
source to share