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?

2
  • 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" ? Commented Jul 4, 2011 at 12:41
  • That's right, exactly what I meant! But android:text="@array/categories.today" doesn't work, other guess? Commented Jul 4, 2011 at 13:05

1 Answer 1

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" /> 
Sign up to request clarification or add additional context in comments.

4 Comments

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/<item1>"
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.
So, actually it is not possible to be done that way, because it doesn't make any sense to have them both:<string name="earth">Earth</string> <string name="moon">Moon</string> and <string-array name="system"> <item>@string/earth</item> <item>@string/moon</item> </string-array>
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 <item name="today" will always represent the exact same string as @string/today

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.