String Resources Android - android

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!

Related

How to store multiple string as a single array in String.xml file

I am learning Andoid Development using Android Programming: The Big Nerd Ranch Guide book. In chapter 2, book develops an Quiz app, where it stores questions in string.xml file as follows:
<string name="question_mideast">The Suez Canal connects the Red Sea
and the Indian Ocean?</string>
The problem with this approach is when I have a lot of strings I (say 1000), I can't call each string by name. A better way is maybe storing strings in an array. So that I can call a particular string as string[2][23] (i.e, string of chapter 2, text number 23).
Could you tell me how to save strings in String.xml files as array?
Is there any better way to deal when you have lots of strings, in terms of performance.
Store it in arrays.xml instead. Check this example file.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="your_string">
<item>A</item>
<item>B</item>
<item>C</item>
<item>D</item>
</string-array>
</resources>
Read it in Java Code as.
String[] mTestArray = getResources().getStringArray(R.array.your_string);
User String-array
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>

How to put ArrayList elements (items) into a TextView?

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

Spannable array from string resource

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

How to get String-Array referenced by another String-Array?

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);

Android: use res/values with String[]

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>

Categories

Resources