15

snip-code from values/string.xml:

<array name="categories">
    <item name="today">Today</item>
    <item name="life">Life</item>
    <item name="corner">Corner</item>
    <item name="banks">Banks</item>
    <item name="it">IT</item>
    <item name="fun">Fun</item>
</array>

snip-code from layout/main.xml:

<Button
    android:id="@+id/today"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="8sp"
    android:text="@array/categories"
    />

Is it possible to assign a label-name to a Button with id=today directly from values/string.xml using string-array with name=categories and specific item, in this case - today?

kapa
  • 77,694
  • 21
  • 158
  • 175
nenito
  • 436
  • 2
  • 5
  • 9
  • Ah so you mean android:text="@array/categories[0]" (this is incorrect) but that's what you want right? Tried android:text="@array/categories.today" ? – Blundell Jul 04 '11 at 12:41
  • That's right, exactly what I meant! But `android:text="@array/categories.today"` doesn't work, other guess? – nenito Jul 04 '11 at 13:05

1 Answers1

19

See the selected answer here: Android - reference a string in a string array resource with xml.

According to that answer, have to do something like this:

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

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

Then you would simply do:

  <Button ....
  android:text="@string/earth" />
Community
  • 1
  • 1
Fraggle
  • 8,607
  • 7
  • 54
  • 86
  • 3
    I think you misunderstand me. I want to get the value of a single array-item declared in **values/string.xml** file and put that value for a text of a button. `android:text="@array/categories/"` – nenito Jul 04 '11 at 12:43
  • 4
    Right, the technique shown above shows how to do that. You just have to change your array values to be structured like the above. Just take another look and try it. Read the answer in the link I posted. – Fraggle Jul 04 '11 at 12:50
  • 2
    So, actually it is not possible to be done that way, because it doesn't make any sense to have them both:`Earth Moon` and ` @string/earth @string/moon ` – nenito Jul 04 '11 at 12:55
  • 1
    The point of having them both mean's you can reference the singular string "today" in your XML file. Then within your code you can reference the array and know that the value you use for – Blundell Jul 04 '11 at 13:22