Defining string resources

Up till now, we have been passing hardcoded strings as values to element attributes that require text to be set. This is not best practice and generally should be avoided. Instead, string values should be added in a string resource file.

The default file for string resources is strings.xml and this can be found in the res | values directory:

String values are added as string resources using the <string> XML tag. We need to add string resources for all string values we have used thus far. Add the following code to your string resource file:

<resources>
<string name="app_name">Tetris</string>
<string name="high_score_default">High score: 0</string>
<string name="new_game">New game</string>
<string name="reset_score">Reset score</string>
<string name="exit">exit</string>
</resources>

Now it is necessary to edit our MainActivity layout file to exploit these created resources. A string resource can be referenced with @strings/ prefixing the string resource name. Consider the following code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mydomain.tetris.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="@dimen/layout_margin_top"
android:layout_marginBottom="@dimen/layout_margin_bottom"
android:orientation="vertical"
android:gravity="center">
<!-- Aligns child elements to the centre of view group -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textAllCaps="true"
android:textSize="80sp"/>
<TextView
android:id="@+id/tv_high_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/high_score_default"
android:textSize="20sp"
android:layout_marginTop="@dimen/layout_margin_top"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center">
<!-- Aligns child elements to the centre of view group -->
<Button
android:id="@+id/btn_new_game"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/new_game"/>
<Button
android:id="@+id/btn_reset_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reset_score"/>
<Button
android:id="@+id/btn_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/exit"/>
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.144.108