0

Preparing string resources for my Android application, I've faced such a problem. I need two string arrays with 12 and 13 items, and first 11 items are common for both arrays. It would be a good idea to create array of 11 common items, then create two needed arrays contained 11 items from the 'common' array plus 1 and 2 additional items. However, I cannot find a way to do it. It is possible to reference to some xml resource, but in my case it makes necessary to include each 11 common items separately.

Thanks in advance for any idea

5
  • Do you use kotlin or java? Commented Jan 1, 2020 at 22:45
  • Also, where do you want to get reference to final array items? Commented Jan 1, 2020 at 22:48
  • Natig Babayev, thanks for replies. Java is used. I need reference to other array in XML while creating XML. Id est, here is xml string-array with common 11 items, and it is the base for 2 other string-array's. Commented Jan 1, 2020 at 22:58
  • What will you do with that another string array? Are you going to use it from Java code or somewhere else? Commented Jan 1, 2020 at 22:59
  • 1
    Natig Babayev, thanks for trying help. It is android app. Two other arrays which include 11 items from the base array, are the sources for ArrayAdapter's in java code. Commented Jan 1, 2020 at 23:02

1 Answer 1

2

In your XML, you can reference common array as item:

<resources>

    <string-array name="common_array">
        <item>name 1</item>
        <item>name 2</item>
        <item>name 3</item>
    </string-array>
    <string-array name="new_array">
        <item>@array/common_array</item>
        <item>name 4</item>
    </string-array>
</resources>

Then in your java code, you add following function which will convert the resource items to list of strings:

List<String> getArrayItems(int resourceId, Resources resources) {
    ArrayList<String> itemList = new ArrayList<>();
    TypedArray typedArray = resources.obtainTypedArray(resourceId);

    for (int i = 0; i < typedArray.length(); i++) {
        int type = typedArray.getType(i);
        if (type == TypedValue.TYPE_STRING) {
            itemList.add(typedArray.getString(i));
        } else if (type == TypedValue.TYPE_REFERENCE) {
            int resIdOfArray = typedArray.getResourceId(i, 0);
            List<String> itemsForGivenRes = Arrays.asList(resources.getStringArray(resIdOfArray));
            itemList.addAll(itemsForGivenRes);
        }
    }

    typedArray.recycle();
    return itemList;
}

Finally, you can use that function to get list of items in new_array:

List<String> arrayItems = getArrayItems(R.array.new_array, getApplicationContext().getResources());
Log.d("MyAdapter", "onCreate: " + arrayItems.toString()); // should print MyAct: onCreate: [name 1, name 2, name 3, name 4]

Note that in my example, I have considered string arrays only. With small change, you can adapt function to different arrays as well.

I hope my answer helps.

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

1 Comment

Natig Babayev, thanks for reply. Of course, I tried to insert common array's items, but without success. When common array is included as an element, the list (in fact - spinner) seems invalid because app is stopped when launching. Your approach is good, but I'm going on another way. 11 common items are saved in separate file strings-common.xml (it is possible and must be done for all my 3 locales), then these items are referenced 11 times. Some complicated, but the main aim is achieved: if there will be need to change one of common items, it will be done only in strings-common.xml.

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.