5

How to access an element of a string array

<string-array name="contextMenuItems">
    <item>Edit</item>
    <item>Info</item>
    <item>Delete</item>        
</string-array>

from another resource xml file

<menu>
    <item android:id="@+id/Edit"
        android:title="@string/contextMenuItems" />
    <item android:id="@+id/Info"
        android:title="@array/contextMenuItems" />
</menu>

3 Answers 3

3

It looks like it can't be done according to the people in this similar post.

How to access <item> from <string-array> resource in an android XML?

My suggestion would be to populate the menu items with some Java code when loading the particular activity or use separate @string values for each menu item.

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

Comments

2

I would like to re-organize the string resource like below

in the file strings.xml,

<string name="label_edit">Edit</string>
<string name="label_info">Info</string>
<string name="label_delete">Delete</string>

in the file arrays.xml

<string-array name="contextMenuItems">
        <item>@string/label_edit</item>
        <item>@string/label_info</item>
        <item>@string/label_delete</item>        
</string-array>

in the file menu.xml

<menu>
    <item android:id="@+id/Edit" android:title="@string/label_edit" />
    <item android:id="@+id/Info" android:title="@array/label_info" />
</menu>

Hope it helps.

Comments

0

you have to override a function:

public boolean onOptionsItemSelected(MenuItem item) 
       {
        // Handle item selection
        switch (item.getItemId()) 
        {
            case R.id.Edit:
                //Handled [0]
                return true;
                case R.id.Info:
                    // Handled [1]
            default:
                return super.onOptionsItemSelected(item);
        }
        }

Comments

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.