Debashish Panda
Debashish Panda

Reputation: 101

How to solve this issue of Hardcoded string?

[I18N] Hardcoded string "Happy Birthday Debashish", should use @string resource less... (Ctrl+F1)

Hardcoding text attributes directly in layout files is bad for several reasons: * When creating configuration variations (for example for landscape or portrait)you have to repeat the actual text (and keep it up to date when making changes) * The application cannot be translated to other languages by just adding new translations for existing string resources. In Android Studio and Eclipse there are quickfixes to automatically extract this hardcoded string into a resource lookup.

enter image description here

Upvotes: 7

Views: 103213

Answers (5)

Boot Systems Go
Boot Systems Go

Reputation: 99

This is only a warning. The function will still work as intended. It is just recommended to place your text in the strings.xml file. This makes future changes much simpler and is easier to reference across multiple pages.

First, place the <string> element in values/strings.xml like this:

<string name="your_string_name">Happy Birthday Debashish</string>

Then, you can reference the string in the .xml file as follows:

<TextView
        android:text="@strings/your_string_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

Again, it is not required to do it in this method. It just makes things simpler to manage and change in the future if needed.

Upvotes: 8

Bmuig
Bmuig

Reputation: 1089

Ths is not an error but a warning. As a general rule, you should never use hard-coded strings in your layout but always use string resources instead (which means that all the strings are stored in one separate file where they are easily changeable for different languages and so on).

To convert a hard-coded string into a string resource:

  • Put the curser on the hard coded string;
  • Press ALT + Enter;
  • Enter a name for your ressource;
  • Click OK.

After doing this the warning will be gone.

Upvotes: 20

sialee
sialee

Reputation: 11

When you are in the 2019 version. Go to the strings.xml and Add this in to it

<string name="Your text">Your Text</string>

Or

In the warning it has the Fix button you can click it for fix

Upvotes: 1

ak-SE
ak-SE

Reputation: 1010

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title"
        tools:text="Happy Birthday Debashish" />

Upvotes: 2

Navin Gupta
Navin Gupta

Reputation: 803

This is just a warning.
Define your string in string.xml file

Happy Birthday Debashish

and in textView use this string as

 <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/string_name"
        />

Upvotes: 9

Related Questions