hardcoded string row three, should use @string resource
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>
what's wrong with hardcoded string in android xml file?
No, you will not get into trouble, but using @string/yourString
in your xml will be a good practice and it will make multi-language support easier.
hardcoded string “Button”, should use @string resource
You shouldn't hardcode the "text" on the widgets use the strings resources ie., strings in the strings.xml to set the text. Declare the "text" you want to display as a string in strings.xml and access it using @string/your_string_name in the layout file.
Warning when declare a string in android
Strings should go to the res/values/strings.xml
file which could look like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World!</string>
<string name="app_name">My App</string>
<string name="ctrl-shift-s">Ctrl-Shift-S</string>
</resources>
Applications can be localized easily: In Eclipse, right-click the res folder, select Android XML Values File, click next. Enter strings.xml, click next. Now you can add various options like language or network code.
Now you can reference your strings using @string/ctrl-shift-s
.
<TableRow>
<TextView
android:layout_column="1"
android:text="how"
android:padding="3dip" />
<TextView
android:id="@+id/textview-command"
android:text="@string/ctrl-shift-s"
android:gravity="right"
android:padding="3dip" />
</TableRow>
Or, within Java code, R.string.ctrl-shift-s
.
The @+id/xyz
creates an ID so you can get the view from code.
All resources are being added to the R
class automatically.
TextView textView = (TextView) findViewById(R.id.textview-command);
if (textView != null)
textView.setText(R.string.ctrl-shift-s);
Related Topics
Garbage Collection Causes:Mediaplayer Finalized Without Being Released
How to Display Gif in React-Native Android App
Creating Different Layout for Android Phone and Tablet
How to Switch Automatically Between Viewpager Pages
Set Language to French in Android Datepickerdialog
How to Stop/Cancel Android Countdowntimer
Android Change Text Color of Items in Spinner
Writing Text File to Sd Card Fails
Firebase Sort by Points Depending on Date
How to Use Broadcast Receiver in Different Applications in Android
Is Deprecated Word the Only Difference Between Fill_Parent and Match_Parent
How to Make Zoomable Linearlayout
Display Fb Profile Pic in Circular Image View in Application
Cli on Dalvikvm Fails on Jni Lib
How to Set an Animated Gif File as Live Wallpaper in Android