2

What i need is to define some string array, then use items of that array as values for text attributes in layout for textfields. Something like this:

arrays.xml:

<string-array name="main_table">
    <item>Mon</item>
    <item>Tue</item>
    <item>Wed</item>
    <item>Thu</item>
    <item>Fri</item>
    <item>Sat</item>
    <item>Sun</item>
</string-array>

layout.xml:

<TextView style="@style/txt.weekdays"   android:text="@array/weekdays[0]" />
<TextView style="@style/txt.weekdays"   android:text="@array/weekdays[1]" />
<TextView style="@style/txt.weekdays"   android:text="@array/weekdays[2]" />
<TextView style="@style/txt.weekdays"   android:text="@array/weekdays[3]" />
<TextView style="@style/txt.weekdays"   android:text="@array/weekdays[4]" />
<TextView style="@style/txt.weekdays"   android:text="@array/weekdays[5]" />
<TextView style="@style/txt.weekdays"   android:text="@array/weekdays[6]" />

Example above doesn't work as i expected and just shows me textfields with text @array/weekdays[0], @array/weekdays[1] and so on. Only if i use array item without index it gets me first array's item for all text fields

<TextView style="@style/txt.weekdays"   android:text="@array/weekdays" />

I'm not asking how to do the trick programmatically, but the way i wrote. I'm not using simple string resources because i need this array in my app anyway in other places, so using string resources will duplicate this array.

kolyaseg
  • 535
  • 5
  • 13
  • Since you don't want to do the trick programmatically and as you're using static data, why don't you just define this values in strings.xml and call them using @string in your xml layout ? I'm not sure what are you trying to do! – Abed Elaziz Shehadeh Dec 14 '14 at 11:10
  • Actually yes, i can do this, but i need this array anyway in my app. So using string resources will just duplicate this array. – kolyaseg Dec 14 '14 at 11:19
  • 2
    Define the individual String resources, and create your array from those. Check here: http://stackoverflow.com/questions/4161256/android-reference-a-string-in-a-string-array-resource-with-xml – Mike M. Dec 14 '14 at 11:24
  • 1
    Thanks @Mike, this could be the solution. I even have seen this discussion before but didn't take it in account for this issue... – kolyaseg Dec 14 '14 at 11:33

1 Answers1

2

Accessing string-array elements from xml is not mentioned in Official Documentation

You can either provide values in code which you don't want. or use each element as a separate string resource

Ahmad Dwaik 'Warlock'
  • 5,953
  • 5
  • 34
  • 56
  • Agree, but for some alternative solutions see https://stackoverflow.com/questions/4161256/referencing-a-string-in-a-string-array-resource-with-xml. – CoolMind Jan 10 '19 at 10:58