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>
The term hardcoded string refers to a string that doesn't depend on the input to the program but it also has connotations of being hard to change, meaning that you might have to edit the code in one or more places to change it.
In one of our previous tutorials, we have discussed the various types of data that an Array can hold. One of the data types that arrays can hold is a string. In this case, the array is called a String array.
Thus, C++ Vectors can be used to create a string array and manipulate the same easily. The vector. push_back(element) method is used to add elements to the vector string array.
Well I will use the second approch, if you need to localize
(translate it in more languages) your strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With