4

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?

0

3 Answers 3

4

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.

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

Comments

1

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.

Comments

0

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

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.