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

13,980

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
13,980
Hendra Anggrian
Author by

Hendra Anggrian

Open source enthusiast in awe of Kotlin technologies. Spent most times following new stuff and best practices in GitHub.

Updated on June 14, 2022

Comments

  • Hendra Anggrian
    Hendra Anggrian almost 2 years

    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.