vladimir.vivien
vladimir.vivien

Reputation: 522

Is it possible to refer to Array Resource Items directly in XML?

In code, it's easy to use the Resources class to get an instance of an XML typed array resource and traverse its items. My question: is it possible to reference array resource items in XML itself as shown below

<resource>
<array name="items">
  <item>Item One</item>
  <item>Item Two</item>
  <item>Item Three</item>
</array>
<string name="itemThree">@array/items[2]</string>
</resource>

The format shown above does not work. Does anyone know if that is possible using a different format?

Upvotes: 0

Views: 136

Answers (2)

marchica
marchica

Reputation: 2416

I would just define itemThree in Java:

String itemThree = getResources().getStringArray(R.array.items)[2]

Ultimately, the XML gets inflated into Java objects, so it's not much difference IMO.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006789

No, but I think the reverse works. Define your strings as string resources and refer to them as @string/... in the <item> elements.

Upvotes: 1

Related Questions