percy
percy

Reputation: 7

android soon as add a button getting error

Just having a go at androids tutorials done hello world and trying to add a button soon as put button on the main.xml file save the file I get yellow warning

[I18N] Hardcoded string "Button", should use @string resource this come up after saving the file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

Upvotes: 1

Views: 5201

Answers (1)

Boris Strandjev
Boris Strandjev

Reputation: 46943

The warning will not stop you from executing your code. Still it tells you that using hardcoded strings in the layouts is discouraged in Android. If you want to avoid that you need to create file strings.xml in your res\values folder and add in it the following:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="button_string">Button</string>
</resources>

Afterwards change your layout file like that:

...
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_string" />

The convention is to export all literal strings in this file you just created. that way internationalizing your code will be easier too.

By the way you already have example of using strings in your text view, so just add another constant there.

Upvotes: 1

Related Questions