[I18N] Hardcoded string “xxx”, should use @string resource


First, I run this Linear layout for Android in Eclipse Kepler

<LinearLayout
 	android:orientation="vertical"
 	android:layout_width="fill_parent"
 	android:layout_height="fill_parent"
 	android:layout_weight="1">
 	<TextView
 		android:text="Baris pertama"
 		android:textSize="15pt"
 		android:layout_width="fill_parent"
 		android:layout_height="wrap_content"
 		android:layout_weight="1"/>
 	<TextView
 		android:text="Baris kedua"
 		android:textSize="15pt"
 		android:layout_width="fill_parent"
 		android:layout_height="wrap_content"
 		android:layout_weight="1"/>
 	<TextView
 		android:text="Baris ketiga"
 		android:textSize="15pt"
 		android:layout_width="fill_parent"
 		android:layout_height="wrap_content"
 		android:layout_weight="1"/>
 	<TextView
 		android:text="Baris keempat"
 		android:textSize="15pt"
 		android:layout_width="fill_parent"
 		android:layout_height="wrap_content"
 		android:layout_weight="1"/>
 </LinearLayout>

But the triangle icon warn [I18N] Hardcoded string “Baris pertama”, should use @string resource

and same for the rest of the warnings.

After googling, I know that is not good practice to hard code strings into layout files. We should add them to a string resource file (at res/values/strings.xml) and then reference them from your layout.

And the modified strings.xml like this

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Linear Layout</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

    <string name="baris1">Baris pertama</string>
    <string name="baris2">Baris kedua</string>
    <string name="baris3">Baris ketiga</string>
    <string name="baris4">Baris keempat</string>
</resources>

So LinearLayout.xml

<LinearLayout 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_weight="1"> 
        <TextView 
            android:text="@string/baris1" 
            android:textSize="15pt" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:layout_weight="1"/> 
        <TextView 
            android:text="@string/baris2" 
            android:textSize="15pt" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:layout_weight="1"/> 
        <TextView 
            android:text="@string/baris3" 
            android:textSize="15pt"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:layout_weight="1"/> 
        <TextView 
            android:text="@string/baris4" 
            android:textSize="15pt" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:layout_weight="1"/> 
    </LinearLayout> 

But still one more warn,

Nested weights are bad for performance

. The solution is related to Relative Layouts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: