Referencing a String in a String Array Resource with Xml

Referencing a string in a string array resource with xml

In short: I don't think you can, but there seems to be a workaround:.

If you take a look into the Android Resource here:

http://developer.android.com/guide/topics/resources/string-resource.html

You see than under the array section (string array, at least), the "RESOURCE REFERENCE" (as you get from an XML) does not specify a way to address the individual items. You can even try in your XML to use "@array/yourarrayhere". I know that in design time you will get the first item. But that is of no practical use if you want to use, let's say... the second, of course.

HOWEVER, there is a trick you can do. See here:

Referencing an XML string in an XML Array (Android)

You can "cheat" (not really) the array definition by addressing independent strings INSIDE the definition of the array. For example, in your strings.xml:

<string name="earth">Earth</string>
<string name="moon">Moon</string>

<string-array name="system">
<item>@string/earth</item>
<item>@string/moon</item>
</string-array>

By using this, you can use "@string/earth" and "@string/moon" normally in your "android:text" and "android:title" XML fields, and yet you won't lose the ability to use the array definition for whatever purposes you intended in the first place.

Seems to work here on my Eclipse. Why don't you try and tell us if it works? :-)

How to reference to a string array element in XML (android)

It looks like it can't be done according to the people in this similar post.

How to access <item> from <string-array> resource in an android XML?

My suggestion would be to populate the menu items with some Java code when loading the particular activity or use separate @string values for each menu item.

reference string-arrays in android strings.xml

You should do the following :

  //obtain the array that refrences others array
TypedArray plantArray=getResources().obtainTypedArray(R.array.plants);
//obtain the referenced arrays
CharSequence[] ginkoArray=plantArray.getTextArray(0);
//...
CharSequence[] orlayaArray=plantArray.getTextArray(3);

reference string array res id from an array

Your second array should be an array of string arrays, not string array of string arrays.

I think the way you would do what you need is:

<array name="my_strings_arrays">
<item>@array/str_arr1 </item>
<item>@array/str_arr2 </item>
</array>

Referencing an XML string in an XML Array (Android)

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

How to get string from string-array in strings.xml

To get String from strings.xml you should have key for that String.But in the above you have key for String array so you have to get String array from that you have to get required String.

CurrentWebpage = getResources().getStringArray(R.array.link_categories)[0];

Hope this will helps you.



Related Topics



Leave a reply



Submit