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 Build a Simple Android App (2014) Creating the Screen Layout Using a Fullscreen Theme

Is an improper use of strings causing my app to not run?

My code is not running. I have no errors marked in my code prior to running, but i did have warnings in regards to strings. Below is one of the lines which was marked as a warning. Is my syntax correct? If so, what is causing this error? Thank you. ''' android:text="Did you know?" '''xml

1 Answer

Jaiden Cruger if I'm understanding your question correctly, it's that you have a warning in one of your .xml files. The warning you are seeing is probably a "hardcoded string" warning. If you hover your mouse over the xml you pasted you'll see the full warning message. You should see something like "Hardcoded string "Did you know?", should use @string resource".

The reason for this warning is that you shouldn't be using hardcoded strings in your application (java or xml), and instead use the strings.xml to keep all of your strings in one place. Two use cases where this comes up is 1. If you want to translate your application into other languages 2. If you constantly hardcode a value (let's say your app name) in multiple places, and then you decide to change your app name, you'll have to manually go and change that value everywhere. By using the strings.xml file to declare your strings you can solve the use cases mentioned.

Now, how do you go about fixing this?

One way you could do this in Android Studio is by clicking on the warning to place your cursor there, and then hit Alt + Enter. You should see a little window that lists possible actions. At the top of this list it should say "Extract string resource". Select that by hitting Enter and type in a valid name for the "Resource name" field and hit ok. You should see the xml update in front of you, and in the background, Android Studio has created a string resource for you in the strings.xml file.

The second (manual way) of doing this, would be to open up your strings.xml file and add a line <string name="example">Did you know?</string> and then updating your xml to be android:text="@string/example"

If you still need some help, let me know!