I have a ArrayList in my strings.xml. I would like to choose elements from it and put it in a .setText(). How do I this?
Example: XML file saved at res/values/strings.xml:
<?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>
This application code retrieves a string array:
String[] planets = getResources().getStringArray(R.array.planets_array);
Assigning the value to a textView:
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(planets[1]); //Venus
Read more at:
http://developer.android.com/guide/topics/resources/string-resource.html
Should ArrayList be declared in arrays.xml?If so, use Context.getResource().getStringArray() to choose elements
Related
Is there a way to extract a Spannable[] stuff from string resource? Basically something similar to
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Welcome to <b>Mercury</b></item>
<item>Welcome to <b>Venus</b></item>
<item>Welcome to <b>Earth</b></item>
<item>Welcome to <b>Mars</b></item>
</string-array>
</resources>
Then I would extract the array into
Resources res = getResources();
Spannable[] planets = res.getStringArray(R.array.planets_array);
But right now eclipse is telling me to change Spannable[] to String[].
use getTextArray, it returns CharSequence[] instead of String[] which preserves attached spans
There are many methods for getting Key & Value from Array.
But what is best way getting Key & Value from an Array in arrays.xml like key => value via spinner?
arrays.xml:
<resources>
<string-array name="settings_listofitems">
<item name="1">item1</item>
<item name="2">item2</item>
</string-array>
</resources>
When i use code in below i get only item value. But i need item key value too:
Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
String itemvalue = getResources().getStringArray(R.array.settings_listofitems)[spinner1.getSelectedItemPosition()];
Any help is highly appreciated.
You can have Keys and Values both in separate Arrays, like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="settings_listofitems_keys">
<item>1</item>
<item>2</item>
</string-array>
<string-array name="settings_listofitems_values">
<item>item1</item>
<item>item1</item>
</string-array>
</resources>
And then parse them like this :
Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
String itemvalue = getResources().getStringArray(R.array.settings_listofitems_values)[spinner1.getSelectedItemPosition()];
String keyvalue = getResources().getStringArray(R.array.settings_listofitems_keys)[spinner1.getSelectedItemPosition()];
I hope this helps.
I'm trying to about the Strings Resources of Android. I'm learning from the Android's developper guide
At the section String Array I'm trying the examples that they gives:
<?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>
in my .java I put this code:
...
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
I'm getting an error at the "array" in my .java file. Anyone has a clue?
Thanks!
For example if I have the following String_Array
<string-array name="recipe_ingredients">
<item>#array/m1_ingredients</item>
<item>#array/m2_ingredients</item>
</string-array>
How can I access #array/m1_ingredients where it is another String_Array
EDIT :
No, every item of a StringArray must always be a String, of course. So it can't be another StringArray
check the Documentation on resources, scroll to StringArray
name
String. A name for the array. This name will be used as the resource ID to reference the array.
here's an eg
<?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>
This application code retrieves a string array:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
I know how to use strings.xml for global strings in Android, but what's about the String[] ?
Is there any possibility to use this and if there is, how?
Thank you.
If you meant String arrays inside and xml refer here
Create a file in your values folder called arrays.xml and create your array like this
<string-array name="colors">
<item>red</item>
<item>orange</item>
<item>yellow</item>
<item>green</item>
<item>blue</item>
<item>violet</item>
</string-array>
Not Directly in strings.xml, but something like this can be done in string.xml:
<string name="string1">str1</string>
<string name="string2">str2</string>
<string-array name="system">
<item>#string/string1</item>
<item>#string/string2</item>
</string-array>