hardcoded string "row three", should use @string resource
It is not good practice to hard code strings into your layout files/ code. You should add them to a string resource file and then reference them from your layout.
- This allows you to update every occurrence of the same word in all
layouts at the same time by just editing yourstrings.xml
file. - It is also extremely useful for
supporting multiple languages
as a separatestrings.xml file
can be used for each supported language - the actual point of having the
@string
system please read over the localization documentation. It allows you to easily locate text in your app and later have it translated. - Strings can be internationalized easily, allowing your application
to
support multiple languages with a single application package file
(APK).
Benefits
- Lets say you used same string in 10 different locations in the code. What if you decide to alter it? Instead of searching for where all it has been used in the project you just change it once and changes are reflected everywhere in the project.
- Strings don’t clutter up your application code, leaving it clear and easy to maintain.
It is not good practice to hard code strings into your layout files. You should add them to a string resource file and then reference them from your layout.
This allows you to update every occurrence of the word "Yellow" in all layouts at the same time by just editing your strings.xml file.
It is also extremely useful for supporting multiple languages as a separate strings.xml file can be used for each supported language.
example: XML file saved at res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="yellow">Yellow</string>
</resources>
This layout XML applies a string to a View:
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/yellow" />
Similarly colors should be stored in colors.xml and then referenced by using @color/color_name
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Black">#000000</color>
</resources>
You must create them under strings.xml
<string name="close">Close</string>
You must replace and reference like this
android:text="@string/close"/>
Do not use @strings even though the XML file says strings.xml or else it will not work.