What is the best way to use an array of hard-coded arrays or resources?

What is better to use in my Android program:

in mainActivity.java:

String[] st = {"Value1","Value2"}; 

or

in mainActivity.java:

String[] st = (String[]) getResources().getStringArray(R.array.Array1); 

And in strings.xml:

<string-array name="Array1">
    <item >Value1</item>
    <item >Value2</item>
</string-array>
+5
source share
3 answers

Well, I will use the second convention if you need localize(translate it into several languages) your lines.

+8
source

The second solution is better, because later you can use different languages ​​(English, German, etc.) http://developer.android.com/guide/topics/resources/localization.html

+2
source

I also suggest using the second, like the others. In addition, it will also help you if you want to change the elements of an array or want to add / remove elements of an array. In this case, you do not need to change the source code again and recompile it. Instead, you just need to change only the XML file and make it.

0
source

All Articles