Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Hardcoded string xxx should use @string resource" issue

Tags:

android

I'm getting the following error in hello world activity code:

Description Resource Path Location Type
[I18N] Hardcoded string "and this is a clickable button!", 
    should use @string resource
activity_hello_world.xml/HelloWorld/res/layout line 21 Android Lint Problem

Please help me out.

like image 948
user1795999 Avatar asked Nov 03 '12 09:11

user1795999


People also ask

Why should you use string resources instead of hard coded strings in your apps?

It allows you to easily locate text in your app and later have it translated. Strings can be internationalized easily, allowing your application to support multiple languages with a single application package file (APK).

What are hardcoded strings?

Hardcoded string are the literal strings. So, What you may be referring to is, literal strings in the data. C# // Instead of var myStr = Home + "123"; // Using this var myStr = "Test123"; The problem doesn't show up, unless we have to use this string literal in multiple places.


1 Answers

This is not an error, this is a Lint warning. So you can run the App, but the recommended way to display texts(on TextViews, Buttons etc.) is to use string references. You have to go to your res folder and there will be a strings.xml file under values. Add this in there:

   <string name="my_string">Your Text!</string>

And to set text on you button you then have to do this:

android:text="@string/my_string"
like image 187
Ahmad Avatar answered Sep 27 '22 22:09

Ahmad