Android: use res/values with String[] - android

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>

Related

The item in string-array can be directly localized in android?

I know I can use string resource to localize item in string-array, just like Method 1 do.
Is it OK to localize the item in string-array directly ? just like Method 2
Thanks!
--------------------------Method 1-----------------------------------------
<string-array name="Box">
<item>#string/Inbox</item>
<item>#string/Sent</item>
<item>#string/Outbox</item>
<item>#string/Draft</item>
</string-array>
<string name="Inbox">Inbox</string>
<string name="Sent">Sent</string>
<string name="Outbox">Outbox</string>
<string name="Draft">Draft</string>
<string name="Inbox">收件</string>
<string name="Sent">发件</string>
<string name="Outbox">已发</string>
<string name="Draft">草稿</string>
-----------------------------Method 2-----------------------------------------
<string-array name="Box">
<item>Inbox</item>
<item>Sent</item>
<item>Outbox</item>
<item>Draft</item>
</string-array>
<string-array name="Box">
<item>收件</item>
<item>发件</item>
<item>已发</item>
<item>草稿</item>
</string-array>
yes you can use Method2.
you just have to create localized resource folder.
here is a complete reference.

How to get the count of array from resources file?

I have multiple arrays in resources like this:
<resources>
<string-array name="myArray1">
<item>String1</item>
<item>String2</item>
<item>String3</item>
</string-array>
<string-array name="myArray2">
<item>String1</item>
<item>String2</item>
<item>String3</item>
</string-array>
<string-array name="myArray3">
<item>String1</item>
<item>String2</item>
<item>String3</item>
</string-array>
</resources>
Now how to get the count of these array dynamically?currently I have 3 arrays.
I can get the particular array items like this
String[] resourceString =getResources().getStringArray(R.array.myArray);
But how to get the count?
int length =getResources().getStringArray(R.array.myArray).length;
The XML file is not for counting the arrays inside it. It is for storing information that doesn't belong in your code in a static way.
This way you can alter menu's, lists dynamically.
What you can do is instead of creating multiple arrays, is store it ** multidimensional**. link
Now you can count the arrays like user1969053 is suggesting

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

how to use color in arrays.xml file

I have items in my arrays.xml file, and I want only "Bravelle", "Cetrotide", and "Clomid" to use a blue color. How can I get this?
<string-array name="medication_name">
<item>Bravelle<sup>®</sup> (urofollitropin)</item>
<item>Cetrotide<sup>®</sup> (cetrorelix acetate)</item>
<item>Clomid<sup>®</sup> (clomiphene citrate)</item>
</string-array>
Thanks in advance.
Try adding <font color="#0000FF"> and the corresponding </font> tag.
Try this:
<string-array name="medication_name">
<item>Bravelle<font fgcolor="#ffff0000">red</font><sup>®</sup> (urofollitropin)</item>
<item>Cetrotide<font fgcolor="#ffff0000">red</font><sup>®</sup> (cetrorelix acetate)</item>
<item>Clomid<font fgcolor="#ffff0000">red</font><sup>®</sup> (clomiphene citrate)</item>
</string-array>

Referencing an XML string in an XML Array (Android)

in arrays.xml
<string-array name="my_items">
<item>My item 1</item>
<item>My item 2</item>
<item>My item 3</item>
</string-array>
in strings.xml
<resources>
<string name="item1">My item 1</string>
<string name="item2">My item 2</string>
<string name="item3">My item 3</string>
</resources>
I would like to reference the string in the array "My item 1" from strings.xml. How do I do that?
oh yeah, that is what I meant. This is how I did it.
<string-array name="my_items">
<item>#string/item1</item>
<item>#string/item2</item>
<item>#string/item3</item>
</string-array>
It resolved correctly in Android 1.6
You can't. It might be possible to do the reverse: have #string/item1 in the <string-array>.

Categories

Resources