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.

4
  • 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! Commented Dec 14, 2014 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. Commented Dec 14, 2014 at 11:19
  • 2
    Define the individual String resources, and create your array from those. Check here: stackoverflow.com/questions/4161256/… Commented Dec 14, 2014 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... Commented Dec 14, 2014 at 11:33

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Agree, but for some alternative solutions see stackoverflow.com/questions/4161256/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.