Strings
This is another XML file that contains all text that your application uses.
For example, names of buttons, labels, default text and all such strings go into this file.
This is the best practice of separating concerns of various files, even if they are XML files.
In other words, layout XML is responsible for layout out widgets, and strings XML is responsible for their textual content.
The following file is used in this application:
HelloWorld/app/src/main/res/values/strings.xml
|
|
<resources>
<string name="app_name">HelloWorld</string>
<string name="home_page">Home Page</string>
<string name="next_page">Next Page</string>
<string name="action_settings">Settings</string>
</resources>
|
Consider the following layout XML code:
HelloWorld/app/src/main/res/layout/activity_main.xml
|
|
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:weightSum = "1"
android:gravity = "center">
<TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:textAppearance = "?android:attr/textAppearanceLarge"
android:text = "Hello, your name:"
android:layout_weight = "0.07"
android:textStyle = "bold" />
<EditText
android:layout_width = "216dp"
android:layout_height = "wrap_content"
android:id = "@+id/name" />
<Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "@string/next_page"
android:id = "@+id/next" />
</LinearLayout>
|
The attribute value
@string/next_page
, where
string
is the resource type and
next_page
is the resource name, represents the string “Next Page,” stored in the file:
/res/values/strings.xml
You can use this syntax in an XML resource any place where a value is expected that you provide in a resource.
The desire to reach for the stars is ambitious.
The desire to reach hearts is wise.
― Maya Angelou
|