Anton Baryshnikov
Anton Baryshnikov

Reputation: 65

What is better to use, hardcoded array or resources string array?

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>

Upvotes: 6

Views: 5120

Answers (3)

Chandra Sekhar
Chandra Sekhar

Reputation: 19502

I will also suggest to use the 2nd one as others suggested. Additionally it also helps you, if in case you want to change the elements of the array or if you want to add/delete elements of the array. In this case you don't need to change the source code again and recompile it. Instead you just need to change the XML file only and its done.

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157457

Well I will use the second approch, if you need to localize (translate it in more languages) your strings.

Upvotes: 9

David
David

Reputation: 4117

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

Upvotes: 2

Related Questions