MLQ
MLQ

Reputation: 13511

Why reference strings from an external resource file instead of hard-coding it to your Android XML layout?

I keep getting warnings in Eclipse for hard-coding strings in my Android XML layout, but I think it makes more sense than putting everything in a string resource file and referencing it from there. I'm only going to use the said strings for that Activity anyway, and never again.

Are there any dangers into this kind of practice, like maybe initialization errors or performance issues, that I am overlooking? Why does Android encourage using a separate resource file?

Upvotes: 4

Views: 1229

Answers (3)

Chirag
Chirag

Reputation: 56925

These files provide a central location for static application-wide data. Separating this data from the main application code could be seen as being beneficial to the overall application structure

There are a number of benefits to including string content as a resource, such as:

  1. It centralizes the strings used by the application in a single location that is easily managed (by the developer or a non-developer).
  2. Strings can be defined as a resource once, and used throughout the code. Therefore, it will have consistent spelling, case and punctuation.
  3. Strings can be internationalized easily, allowing your application to support multiple languages with a single application package file (APK).
  4. Strings don’t clutter up your application code, leaving it clear and easy to maintain.

Upvotes: 1

Romin
Romin

Reputation: 8806

Keeping strings separate from your code and in resource files has a lot of advantages and is generally considered to be good practice.

First, it will keep all your strings in one place, so there is a quick way for you to maintain them in one file.

The most important reason is that of Internationalization. If you application has a need to support multiple languages, then separating out your strings (test, button labels,etc) in different resource files for different languages helps to manage this process much better.

For more information, refer to http://developer.android.com/guide/topics/resources/localization.html

Upvotes: 0

Graham Borland
Graham Borland

Reputation: 60681

The main reason is for internationalization. Putting strings in resource files makes it much easier to provide separate translations of each string for different languages, without having to copy your layout files.

Upvotes: 4

Related Questions