Setting the contents of the Spinner using a String array in code is a good way to see how the Spinner works but it isn't the way it usually happens.
Android provides a comprehensive system of resources - strings, images and lots of XML files. The idea is that you can produce customized versions of you application just by changing the resource files i.e. without having to modify your code.
For detailed information on using resources see chapter 9 - Resources
For example in the case of a Spinner you could set up a resource that was a String array that provided the list of items to be displayed. If you then wanted to create a version of your app for a foreign market you could get the list translated and created a resource in the new language.
Resources are a great idea and you should use them for all fixed strings and fixed data in general. So far the examples have tended to avoid resources to make things simpler but for a Spinner you need to know how to code up a String array resource and use it.
Android Studio does have some good resource support but in some areas it is lacking. For example, in an ideal world the system would help you create a String or String array resource but at the moment it only goes so far. While it does help you create a String resource it does nothing for a String array and we have no choice but work with the XML file.
That is - at the moment Android Studio only provides help for simple string values.
Find the file strings.xml in the res/values directory and open the file strings.xml. Add to this the String array definition:
The meaning of the XML is obvious and this is the advantage of a human readable markup language.
If you have explored resources using the Resource window which appears when you select the three dots option in the properties window you might worry that this new resource i.e. SpinnerList, doesn't appear. It also doesn't appear in the Resource editor that you can select while editing the XML file.
The reason is that currently Android Studio doesn't support the assignment, editing or creation of String Arrays other than manually.
However making use of the new resource is fairly easy.
To create an ArrayAdapter from a resource you need to make use of a static method of the ArrayAdapter class - createFromResource. This just needs you to specify the context, the resource id and the Spinner layout. All you have to do is to replace the creation of stringArrayAdapter
With this change everything works as before but now to change the items that appear in the Spinner you simply edit the XML file.
You could also use the getResources object to retrieve the String array and then proceed as if the String array had been defined in code.
private String country = getResources().getStringArray(R.array.SpinnerList);
Changing The List
There are lots of more advanced things that you can do with Spinners but these aren't encountered that often - mainly when trying to build a custom user experience.
The one thing that does occur often is the need to dynamically change the list of items. There are many slight variations on this but essentially what you can do is change the String array and then call the adapter's notifyDataSetChange method. For example if you want to change Mexico, i.e. element one, to Greenland you would use: