1

While working on an app, I noticed that whenever I don't use "@string/"...

android:text="@string/stringName"

and just write

android:text="stringName"

Eclipse want's me to add back "@string/". Why is that? What's wrong with hardcoding the string?

2
  • 1
    Basically for the ease of adding more language support. Also, let's say you name your app "ABC" and you write the name of your app everywhere, and then you change your app name to "CBA" using a string resource will be a lifesaver. Commented Apr 2, 2013 at 0:40
  • Localisation of the application. Commented Mar 11, 2014 at 11:23

1 Answer 1

4

You can easily translate your app if you use string resources. For example you can translate it into german, just by putting your string resources into the values-de folder and translating them. Android will then pick the right strings based on the current locale of the phone. It also increases the efficiency of the app.

This is what it can look like:

/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Simple Calculator</string>
    <string name="welcome_message">Welcome!</string>

</resources>

/values-de/strings.xml (german):

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Simpler Taschenrechner</string>
    <string name="welcome_message">Willkommen!</string>

</resources>

Edit:

Just like @EGHDK mentioned; it saves you a lot of time, if you want to change some text. Using string resources you have everything in one place.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.