Where to store string values? In strings.xml or in a constants class?

In android we can store string values โ€‹โ€‹either in strings.xml file or in some constants class as a static final variable. Is there any reason for choosing one of these in some cases?

+3


source to share


6 answers


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.

+5


source


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.

+3


source


If the string value is used to display in the UI store, Strings.xml

otherwise, store it in code. Can be JSONTags, Key for different api / Thirdparty libraries. Such things should be kept in the code itself.

+2


source


strings.xml

it is used for localization and needs a context to fetch the content String

. If you need a java constant to access in different classes, you can use element public static final String

. If the string is a message to the user then you should use strings.xml

+2


source


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

+2


source


It depends if it is a text string to be translated or displayed to the user and then 118n you will need to put in strings.xml

.

However, if the string is something like a server url or api code, then you will want to store it in code as public static final String

0


source







All Articles