Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's wrong with hardcoded string in android xml file?

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.

like image 828
Hendra Anggrian Avatar asked Sep 28 '12 05:09

Hendra Anggrian


People also ask

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

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.

What is hardcoded in Android?

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.

What is hard coded string?

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.


1 Answers

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.

like image 119
Zgrkpnr__ Avatar answered Oct 22 '22 02:10

Zgrkpnr__