5

In android, we can store string values either in strings.xml file or in some constants class as static final variable.Is there some reason for selecting one over another in some circumstances?

6 Answers 6

9

In a nutshell:

value for use in code: use always constants class.Advantage: codes remain integrated and your package can be utilized in other projects/contexts. You can not do that with string.xml as it is not transported with your package.

value for display in UI: use string.xml. Advantage: you can use localization to display translated texts.

Some situation may arise when both option appears viable. You will have to then decide where are its related values are stored.

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

Comments

3

As a general rule, use the strings.xml because android uses that XML to enable translating your app into different languages, which it can't do with strings that are hardcoded.

The official android guide on localization say the following;

Move all strings into strings.xml

As you build your apps, remember not to hard code any string. Instead declare all of your strings as resources in a default strings.xml file which makes it easy to update and localize. Strings in strings.xml file can be extracted, translated and integrated back into your app (with appropriate qualifiers) without any changes to compiled code.

If you generate images with text, put those strings in strings.xml as well, and regenerate the images after translation.

Strings that are not going to be displayed to the user in any way needn't be stored in the XML, because they will never need translating, and you probably don't want the android system tampering with them in ways you might not know about during runtime.

Comments

2

If the string value is used to display in UI store in Strings.xml Otherwise keep it in code. There can be JSONTags, Key for different api/Thirdparty libraries.These kind of things should be kept in code itself.

Comments

2

strings.xml it is used for localization and needs a context to retrieve the content of a String. If you need a java constant to be accessed in different classes, you a public static final String member. If the string is a message for the user you should use strings.xml

3 Comments

+1 for mentioning that you need a context to retrieve the contents of a strings.xml string
In case when I want to send a string with intent to email application for the message body, I can store such strings in any of the above locations.Isnt it?Or srings.xml should get preference?
strings.xml would be preferable
2

If strings represent text readable by user, and which could potentially be translated to other languages (names of buttons, labels, notification/error messages, etc.) then they should be in strings.xml (actually, it can be any file name you like, not just "strings"). If string is some constant which is used in the app internally (bundle/intent keys, fragments tags, etc.) they should be declared in class

2 Comments

In case when I want to send a string with intent to email application for the message body, I can store such strings in any of the above locations.Isnt it?Or srings.xml should get preference?
srings.xml should get preference
0

It depends, if it is a text string that will be translated or displayed to the user then for 118n sake, you will want to put in into strings.xml.

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

Comments

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.