I'm new to Android and trying to understand the code I wrote from a tutorial. Meanwhile, I'm referencing the documentation whenever I want to really understand something.
That page discusses string resources.
For a string, under the String section it states I could retrieve a string defined in an XML file like this: String string = getString(R.string.hello);
.
XML in question:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello!</string>
</resources>
Now, for a string array, under the String Array section it states I could retrieve a string string array defined in an XML file like this::
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
XML in question:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
How come I have to do Resources res = getResources();
for a String Array, and not for a String in an XML file?
Would String[] planets = getStringArray(R.array.planets_arry);
not work? Do resources
have more methods, namely for more complex cases like String Arrays? is getString
so simple it is fine to not need it under a resource? Where could I find a list separating what requires me to do Resources res = getResources();
versus what doesn't require that?
Lastly, does getResources
get me every single resource in my Android project irregardless of what XML file it may be in?