I love to put simple text directly on the xml file without declaring a string first. It is easier, more simple and less messy. However, it always has a warning badge whenever I do so.
What if I have a dozens of hardcoded string in my xml file without concerning the warning? Will I get into a trouble?
thanks in advance.
It is not good practice to hard code strings into your layout files. You should add them to a string resource file and then reference them from your layout. This allows you to update every occurrence of the word "Yellow" in all layouts at the same time by just editing your strings.
In computer programming or text markup, to hardcode (less frequently, hard code ) is to use an explicit rather than a symbolic name for something that is likely to change at a later time. Such coding is sometimes known as hardcode (noun) and it is more difficult to change if it later becomes necessary.
The term hardcoded string refers to a string that doesn't depend on the input to the program but it also has connotations of being hard to change, meaning that you might have to edit the code in one or more places to change it.
When you use hard coded strings in both you java code and xml file your application directly writes these strings on RAM. But when you declare them as string resources it will not be written on RAM when application launches but they are written on RAM when application needs to use them. You can declare one billion strings in resources and you can still have a light weight RAM friendly Android application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With