After hours of headache, I figured out how to create and handle a matrix of string resources in Android. And now I’ll share my findings :)

The problem

I’ve been making a quiz app for a while. At the beginning, I faced an interesting problem. Each question of the quiz has a lot of strings relative to it, so I wanted to create an array of strings for each question. But I didn’t know how to do that with string resources, and I didn’t want to create an array in the Activity for each question (that would be terribly inefficient).

The solution

Fortunately, I found the string-array resource. Thus, I was able to create an array of strings for each question.

However, I soon noticed that it would not be enough. Because there are different types of questions, I needed to group the string-arrays of one type of question into another array. That is, I needed an array of string-arrays. An TypedArray resource can hold references to any other resources, including string-arrays.

Now, let’s see how I put it all together.

<!-- ... -->

<!-- Question 3 -->
<string-array name="MAQ_stringArray3">
	<item>Which of the limits below is/are horizontal asymptote(s)?</item>
	<item>\\(\\lim\\limits_{x \\to +\\infty} \\space \\dfrac{2x+1}{x}\\)</item>
	<item>\\(\\lim\\limits_{x \\to -\\infty} \\space \\dfrac{4x^3-2x+1}{2x^4}\\)</item>
	<item>\\(\\lim\\limits_{x \\to -\\infty} \\space \\dfrac{x+3}{2x}\\)</item>
	<item>\\(\\lim\\limits_{x \\to -\\infty} \\space \\dfrac{x^5+2x^2+1}{x^4-3x^2-3}\\)</item>
</string-array>

<!-- Question 4 -->
<string-array name="MAQ_stringArray4">
	<item>In which of the alternatives below does the limit exist?</item>
	<item>\\(\\lim\\limits_{x \\to 0^+} \\space \\dfrac{\\sqrt{x}-1}{x^2}\\)</item>
	<item>\\(\\lim\\limits_{x \\to 0} \\space \\dfrac{x^2+1}{\\sin x}\\)</item>
	<item>\\(\\lim\\limits_{x \\to 2} \\space \\dfrac{5x-4}{x^2-4}\\)</item>
	<item>\\(\\lim\\limits_{x \\to 5} \\space \\dfrac{2x^2+3}{(x-5)^2}\\)</item>
</string-array>

<!-- MultipleAnswerQuestion Array -->
<array name="MAQ_array1">
	<item>@array/MAQ_stringArray1</item>
	<item>@array/MAQ_stringArray2</item>
	<item>@array/MAQ_stringArray3</item>
	<item>@array/MAQ_stringArray4</item>
</array>

<!-- ... -->

You can ignore the KaTeX syntax if you want. I think that you have already figured out that my quiz app is about math, haha.

As you can see, each <string-array> represents a question and contains five strings, and there is an <array> that represents a type of question and contains references to these string-arrays. This same pattern repeats for other question types.

Here’s how I handled those resources in Java:

public class MultipleAnswerQuestion implements Question {

    // ...

    @Override
    public void setViewsText() {
        Resources resources = this.activity.getResources();
        TypedArray MAQArray = resources.obtainTypedArray(R.array.MAQ_array1);
        int MAQStringArrayId = MAQArray.getResourceId(this.questionNumber, 0);

        String[] MAQStringArray;
        if (MAQStringArrayId > 0) {
            MAQStringArray = resources.getStringArray(MAQStringArrayId);

            questionTitle.setText(MAQStringArray[0]);

            for (int i = 0; i < numberOfOptions; i++)
                questionOptions[i].setText(MAQStringArray[i+1]);
        } else {
            Log.v(TAG, "MAQStringArrayId doesn't exist");
        }

        MAQArray.recycle();
    }
	
    // ...
}

A TypedArray is just an array of resources. So, basically, I got the TypedArray defined in the XML (MAQ_array1) to obtain the string array relative to the question, then I went through the string array passing each string to its corresponding View.

And, at the end of this method, I called the TypedArray’s recycle() method, which makes it ready for garbage collection and allows the memory associated with the TypedArray to be re-used by a later caller.

Conclusion

So this is a good grasp about array resources. I hope you’ve liked it. I’ll try to write more short articles to be able to write more frequently. You can see the source code of my app here (I still need to create a README file :P) and feel free to leave a comment below.