Home and Learn: Android Course


Create an array in an Android XML file

When we create our Custom Adapter, we'll pass it an array of headings to go under the images. This array of headings can be set up with Java code. But there is another way - create an array in an XML file. It's quite easy!

Right-click the res > values folder in the Explorer area to the left of Android Studio:

The res > values folder in Android

From the menu that appears, select New > Values resource file. In the dialogue box that appears, type headings_array for the File name:

The dialogue box in Android Studio to add a new resource

Click OK to create a new XML file. You should see this:

<?xml version="1.0" encoding="utf-8"?>

<resources></resources>

Click between the two resources tags. Type a left pointy bracket to see a dropdown list:

A dropdown list of resource types

Select string-array from the list. For the name, type headings then a right pointy bracket. This will complete the string-array tag. Press the enter key between the two tags to give yourself some room:

A string-array tag in an XML resource file

For each item in your array, you need an item tag. So type a left point bracket and then the word item. Type a right pointy bracket to complete the tag. In between your new item tag, type the word Cat:

The item tag in the resource file

Now add the following pets and pet-related terms, one for each item tag:

Dog
Fish
Horse
Rabbit
Parrot
Pigeon
Duck

Your XML file will then look like this:

Items set up in a string-array for an XML resource file

OK, you're done with that file, so you can close it down.

We can get some programming done, now.

Open your MainActivity.java file. Add a String array variable at the top of your code, just above onCreate but below public class …

String[ ] labels;

In the onCreate method, add this line just under setContentView:

Resources res = getResources();

If you get any red text, press ALT + ENTER to add the correct library, or type import android.content.res.Resources; at the top of your code. What we're doing here is getting a reference to the Resource object. We need this to get at our string array.

Next, add this line:

labels = res.getStringArray( R.array.headings ) ;

There is a method of the Resource object called getStringArray. In between the round brackets of getStringArray we have this:

R.array.headings

When you type the dot after R.array, you'll see a popup menu. The array from your headings_array.xml file should be on it.

Your code so far should look like this in earlier versions of Android Studio:

Java code to access an array in an Android XML file

And this in later versions:

Java code to access an array in an XML file in Android Studio 3

The array from the XML file will then end up in the labels arrays.

In the next lesson, you'll create a Custom Adapter that makes use of the array you have just accessed in your Java code.

Back to the Android Contents Page

 


Email us: enquiry at homeandlearn.co.uk