Reputation: 6096
Whenever I make any app I always hardcode the string instead of referencing it from string resource from XML. App works fine but gives me warning to use @string
resource
Example button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click here" />
My question is whether it will affect my application performance if I do so or it(@string
resource) is just used for internationalization.
Upvotes: 18
Views: 4786
Reputation: 40679
As others said, it's for localization, but for performance, it depends on how many times per second those strings are getting looked up. I've seen a case where an app is slow in startup, and stack sampling showed 50% of the time was being spent in resource lookup of strings, and the reason the strings were being looked up was to display them on the splash screen that gives the user something to look at while the app is starting up!
Upvotes: 4
Reputation: 61
Hard coding strings will not affect the performance directly. It affects the maintainability.
In case when you hardcode a string and in later stage if you want to change the string "Click me" to "add" or something else, then you need to search your complete project to change the string where and all it is used. So Better to follow strings.xml always. :)
Upvotes: 1
Reputation: 1967
I dont think hardcoding a string will make your program to run any slower.. Infact it will enhance the performance as there's no need for any lookup for the String in the R.java class.
Referencing the string from strings.xml
is best practice due to 2 reasons:-
1- Localization 2- if you're using the same string in multiple places and would like to edit the same in all the places saves you the overhead of editing all the hard-coded strings individually.
Upvotes: 1
Reputation: 7087
Your app will not support Localization then, if that is not the requirement of your app then there will be no problem using hard coded strings.
Upvotes: 0
Reputation: 54692
It will not create any performance issue. But defining strings in strings.xml is encouraged for the ease of maintainance and lolalization. For example consider the below two scenario.
Scenario 1
When you need to change a string used in many places. In your case You will have to change all the "click here"
in all layouts. But if you declared in strings.xml then only change made in the xml will change them all.
Scenario 2
For another example if you want to show different language for different locale then you need to use the string.xml.
Upvotes: 4
Reputation: 82563
This is an Android lint warning to help you with localization.
Technically, hard coding strings would make your app perform a little better, as it won't have to look up the string from the corresponding R
int each time. However, this performance difference will be negligible, and no human will be able to notice it.
However, you should always keep your String resources in the values folder as it makes localization very easy.
Upvotes: 44
Reputation: 359906
I see no reason that you would get worse performance by using a hard-coded string. There are fewer steps involved with hard-coded strings. However, it is certainly best practice to separate resource strings from application and UI code.
Upvotes: 4