Android working with large arrays – arrays.xml file

Hello Readers,

I hope many of us know about using arrays in Android. Arrays can be of any type, lets say integer or string. Consider a situation where you may have to deal with a string array that has about 100 values. Defining such an array in the code makes it clumsy and unreadable and also increases the number of lines of the code.

The way we actually do is

static String[] how_to_get_ip = {"Step 1: Click on the Start menu",
		"Step 2: Type in the search box as 'Run'",
		"Step 3: Press Enter key",
		"Step 4: In the Run box, type cmd, and press Enter key",
		"Step 5: Now type ipconfig and press Enter key",
	  "Step 6: Now you can see the ip address printed in the screen"};

This may look simple for 6 values. But consider an example where you may have around 100 to 200 values. You are building a word game or dictionary that contains 1000s of words. Simple solution is to create a file called arrays.xml in your project’s res/values/arrays.xml


In the arrays.xml file add the array values as

<?xml version="1.0" encoding="utf-8"?>
    <array name="steps">  
	    <item>Step 1: Click on the Start menu </item>  
	    <item>Step 2: Type in the search box as 'Run'</item>  
	    <item>Step 3: Press Enter key</item>  
	    <item>Step 4: In the Run box, type cmd, and press Enter key</item>
	    <item>Step 5: Now type ipconfig and press Enter key </item>
	    <item>&#8226; Apple </item> <!-- &#8226; denotes the bullet for lists -->
	    <item>&#8226; Orange</item>
	    <item>Don\'t stand </item> <!-- \ for single quotes -->
	    <item>Please use the &lt; or &gt; icons </item> <!-- for < and > symbol -->
	    <item>King \n &amp; \n Queen </item> <!-- For & and \n denotes new line -->
	    <item><u>Underlined text</u> </item> <!-- To show text with underline -->

We can use any kind of special characters, line breaks, bulletins and underline as well.

In order to access these values in Activity class, Use the following code

String[] words = getResources().getStringArray(R.array.steps);
int length = words.length;
for(int i=0;i<length;i++){
System.out.println("From arrayxml:"+words[i]);

We can get the resource from arrays.xml and use it in activity. Our code looks neat and it reduces the number of lines as well. We can group all the arrays in one place and maintenance becomes easy.

Thanks for reading.

Feedback are welcome. Please feel free to ask questions.

Don’t forget to subscribe to Geeks.Gallery, to get free emails to your inbox.

Subscribe to Geeks.Gallery

Follow me


Web Developer at Energy Alternatives India
I'm interested in developing Web applications andkeen on learning new technologies.

View Nagarajan's Profile
Follow me

Latest posts by Nagarajan (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *