On Android, is there a reason to use string resources for strings that won't be translated?

I am wondering what disadvantages are used to use strings defined in java files in Android code.

I like to use plain old Java strings for things that are not visible strings, like for example. the names in the XML documents I'm processing, or the keys for packages. It seems like a good idea to just save all these things in the java file where they are used, instead of moving them to an XML file and making the code more complex.

However, I see many examples of Android code that seem to put every line in the resource file.

What is the problem with having strings in java files? What are the reasons people do not? I do this in my applications and so far I don’t see any problems.

Please note that I know that XML files make sense for things that need to be translated. This question relates to cases where strings remain unchanged.

Let me try to make this question clearer:

Are there any reasons other than:

  • Because it is standard / best practice etc. - My question is mainly: why is this the best practice, just because of i8n, or are there other reasons?
  • Since it allows you to use the resource infrastructure for translation, device-dependent strings, etc.
  • Because it allows the use of non-ASCII characters.
+5
java android android-resources
source share
4 answers

The simple answer to your question is a standard that puts your entire string in a resource. There are also many reasons that if you save your line in an xml / java file, you need to update each link in that file for one line.

eg. if you want to change “Ok” to “confirm”, which are used in 5 different files, you need to change in all these 5 files, but for the String resource you just need to update one file that contains string.xml.

Edit

Below are a few reasons why we should use String.xml

1) To update a single link to multiple entries. As with @treesAreEverywhere This can be done using the public static String, but when you start the application, you will need memory before the application closes. But String written in String.xml will be loaded during use.

2) Support for multiple languages. You can create several language resources to support your multilingual application, so that the language changed using Locale will be dynamically supported by the OS at runtime according to the language folder of the resource.

3) Please check the Localization document, which will give you more information about using String.xml

4) Lines do not clutter up your application code, leaving it clear and easy to maintain.

This is a kind of coding standard, like any other language. But you can ignore it if you want, and you can create your code with an open static string variable in the code. It is not necessary to use String.xml, but its good coding practice to use it. It’s good practice, for example, to close the if block with a parentes containing only one statement, rather than leaving it as it is.

if(condition){ statement; ) if(condition){ statement; ) , not if(condition) statement;

+3
source share

In fact, good practices are a good reason for this, but there are more.

For example, one of the reasons that I can now recall is that strings.xml is encoded in UTF-8. Hard-code strings do not display some characters properly.

+1
source share

The purpose of the strings.xml file (and other * .xml resource files) is to rearrange similar values ​​in one place. This makes it easier to find values ​​that would otherwise be buried in code. These resource files also improve maintainability , since modifying a single value can have application effects (for example, changing the name of an application or theme). Finally, as you already mentioned, this provides the basis for translating your application into other languages.

If you know that your application will not be translated and will not be changed, it is not bad to hardcode them. However, if you think that your application will receive many updates, it is better to start using good funds and using XML resource files.

Apart from these reasons and those @Zinc mentioned (which I don’t know and cannot confirm), there are no other reasons why you would like to use XML resource files.

The disadvantage of using resource files is theoretically slower and requires a bit more memory. Read android - strings.xml against static constants and Does hard string coding affect performance?

+1
source share

If you put all your lines associated with your application, you can very easily implement applications like I18N, and this is very useful when making changes to the application (merging a company with another company). These are just the names of the changes in xml files. No need to touch any java file.

-one
source share

All Articles