JibW
JibW

Reputation: 4562

Android Widgets Naming Standards

In my Android application, for example when I am inserting a Button and adding a static value for it as follows:

android:text="MY BUTTON"

It gives a warning saying Hardcoded string "MY BUTTON", should use @string resource.

I am currently trying to extend some functionality in a previously developed app; do I have to change all those statically defined values to refer to the string.xml file?

Please can anyone give an opinion on the standard? Thanks in advance.

Upvotes: 2

Views: 319

Answers (2)

Jay Sidri
Jay Sidri

Reputation: 6406

The warning you are getting is via the Android Lint utility.

Generally speaking, you ought to get into the habit of using string resources (as Mark Allison wrote) and never 'hard code' string literals in code or XML markup like you have done there regardless of whether you intend to internationalize your app or not.

It just makes sense in terms of future code maintenance.

In fact, I have setup my linter settings to mark hard coded strings as errors so I'm always mindful of it.

To do that, go to Project -> Preferences, expand Android, select Lint Error Checking and change "HardCodedText" from "Warning" to "Error".

Upvotes: 0

Mark Allison
Mark Allison

Reputation: 21924

You don't have to fix this at all because it is a warning not an error. Your app will work perfectly well with hard-coded strings.

That said, it is advisable to fix it because it will make translating your app to other languages much easier should that ever become necessary.

Generally I try to always define strings in the strings.xml file because it really doesn't take too long when developing new code. However converting an existing project to use strings.xml may be a lot of work for little gain if you are unlikely to ever need to translate your app.

Upvotes: 3

Related Questions