13

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.

Name is Nilay
  • 2,743
  • 4
  • 35
  • 77
Hendra Anggrian
  • 5,780
  • 13
  • 57
  • 97
  • it is upto u but if u follow coding standards then it is simple to change if required . once think what to post and what not to post. – Yerram Naveen Oct 09 '12 at 06:31
  • 1
    Related: [hardcoded string “row three”, should use @string resource](http://stackoverflow.com/q/8743349/1288) – Bill the Lizard Apr 01 '13 at 01:31

6 Answers6

16

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.

Zgrkpnr__
  • 319
  • 2
  • 9
7

There will not be any trouble with that. But using string.xml to put text in your app is considered as a good programming practice. Because suppose you want to the same text in more than one places in your app. It will be difficult to put the text every time hard coded, especially if the text is somewhat large. Putting the text to a single file means that you have access to the same text from anywhere in you app.

Renjith
  • 5,783
  • 9
  • 31
  • 42
6

No, you will not get into trouble, but using @string/yourString in your xml will be a good practice and it will make multi-language support easier.

Rahul Baradia
  • 11,802
  • 17
  • 73
  • 121
  • 5
    I'm genuinely curious - what are the performance benefits of using `@string`? (Excluding the obvious one about making multi-language support easier) I would have thought that an extra level of indirection would be a minus. – Ken Y-N Sep 28 '12 at 05:50
  • your comment above makes perfect sense, after all it is good to do so. Thanks! – Hendra Anggrian Sep 28 '12 at 06:48
  • 2
    I am asking about how can I sure that it ill improve my app performance when I will add strings in string.xml? Amazing this is selected answer.... – Pankaj Kumar Sep 28 '12 at 07:13
  • 1
    @PankajKumar -- it ll not affect the app performance much if you get warnings. But warning can occur when interfacing with legacy code written before the advent of generics. doesn't mean that warning = consume memory.. – Rahul Baradia Sep 28 '12 at 08:58
6

Your company was just bought out and now you get to read over thousands of lines of code and changer the old name to the new because somebody hard coded strings. You want to internationalize your code so it speaks Spanish easy.just make a new strings.xml only name it strings_es.xml the phone itself is set Spanish of course you would ttranslate but you are done.I can't read the strings cause this phone is so tiny, maybe you could give short descriptions for small phones. Read up on mvc good luck

4

Nothing to worry about those warnings. Maybe the android developer thought it is not a good practice.
But if your application might support multi language one day, using @string/mystring is the best practice.

Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
2

This is in reference to some comments regarding performance (@PankajKumar, @TechEnd, @Ken Y-N):

According to the Google documentation, "String is compiled resource datatype: Resource pointer to a String." (Link: http://developer.android.com/guide/topics/resources/string-resource.html#String)

So, i think this will increase performance by decreasing the memory footprint, although the difference might not be noticeable.

This is because, when we declare a string in the XML file, a pointer to that resource is maintained. Whenever we need to reuse that resource again somewhere else, we just get a pointer to the existing String , thereby saving memory in allocating a new memory for the same String. This is in contrast to using hard coded strings, for which memory will allocated every-time for the same String resource.

Hope this helps. Comments are welcome.

Regards, MM.

Mrinal Mandal
  • 235
  • 2
  • 11
  • I'm not sure about Davlik, but in most JVMs the string pool means you actually reuse the same string anyway, so what you say is not normally true in Java. – CorayThan Mar 29 '14 at 05:44