Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Shiina Mashiro
Shiina Mashiro
757 Points

Hardcoded String

I got warning when i run the app it says "Hardcoded string 'Signals From Mars', should use @string resource.. what should i do to this kind of warning? Thanks!

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Shiina,

this i a common warning and your app will run fine without creating an error.

If you go to app > src > res > values folder you will see a strings.xml file. Open it and see all the string resources of your project.

You can hardcode a String variable like this:

android:text="someText"

or you can create the value of the String as a reference to the strings.xml file so later on you will have the possibility to change the values inside the resource file without changing your code. If many String use a single String resourse, you just change one resourse value and all String in the code will change. Cool right. This is important if you create many layouts with different languages.

The minimalistic strings.xml resourse could look like this:

<resources>
    <string name="hardCodedStringName"> coolHardCodedStringValue </string>
</resources>

Inside your code you can reference the String resource this way:

android:text="@string/hardCodedStringName"

You can change the value of "hardCodedStringName" without changing the code of the reference.

Makes sense?

Grigorij

Shiina Mashiro
Shiina Mashiro
757 Points

Thanks Grijorji! I appreciate the explaination. I get it now :) Thanks a lot!!