Why refer to lines from an external resource file instead of hard coding to your Android XML layout?

I keep getting warnings in Eclipse for hardcoding strings in my Android XML layout, but I think it makes sense than putting everything in a string resource file and referencing it from there. I will use these lines for this activity anyway and never again.

Are there any dangers for this kind of practice, such as initialization errors or performance problems that I ignore? Why does Android encourage the use of a separate resource file?

+4
source share
3 answers

The main reason is internationalization . Placing strings in resource files makes it easy to provide separate translations of each string for different languages ​​without copying layout files.

+4
source

These files provide a central location for static application data. Separating this data from the main application code can be considered useful for the overall structure of the application.

There are several advantages to including the contents of a string as a resource, for example:

  • It centralizes the lines used by the application in one place that is easily managed (by the developer or not by the developer).
  • Strings can be defined as a resource once and used throughout the code. Consequently, it will have consistent spelling, case and punctuation.
  • Strings can be easily internationalized, allowing your application to support multiple languages ​​with a single application package file (APK).
  • Lines do not clutter up your application code, leaving it clear and easy to maintain.
+1
source

Keeping lines separate from your code and resource files has many advantages and is usually considered good practice.

Firstly, it will save all your lines in one place, so you can save them in one file.

The most important reason is internationalization. If your application needs to support multiple languages , then highlighting lines (test, button shortcuts, etc.) in different resource files for different languages ​​helps to better manage this process.

For more information see http://developer.android.com/guide/topics/resources/localization.html

0
source

All Articles