6

What are the rules for using strings.xml resource files versus an interface where String constants would be defined.

I'm assuming that the strings.xml resource file would be used to define non-functional strings to be used for display purposes such as text in various languages, date rendering formats, etc.

And Interface String constants would be used to define regular expression patterns, Intent extras variable name passing definitions, etc.

3 Answers 3

6

Correct. The key is that you can translate the strings in strings.xml - anything written in English (or any other language) and will be exposed to the user should be in strings.xml so you can translate it.

Patterns/regexes should be Strings for better performance, unless those regexes are language-specific.

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

10 Comments

Patterns and regexes in strings.xml? Please tell me more!
Whoops. Typo. I meant the opposite :P
Hehe. Great, now it makes sense.
I'm trying to determine where that fine line is. I'm tempted to put email regex patterns to match specific domains in the strings.xml file.
@Turbo - this is related: stackoverflow.com/questions/8407340/… In any case, your strings will be easily accessible to anyone, be it in the strings.xml (very easy, it's all in one spot) or as a static string (also pretty easy). That's why they recommend some sort of simple encryption. It can still be bypassed, it'll just take whoever is cracking your app a bit longer.
|
0

yes, multilanguage constant in string.xml and I like to put other constants in a final class like this one:

    public final class Consts {
    public static final String URL = "http://www.google.com";
    }  

and calling them this way:

    Consts.URL;

Comments

-2

String str = getString(R.string.my_string_name); will do the trick. It is very convenient to store all Strings in one place (or separate XML files based on your own needs).

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.