Tuesday, June 2, 2015

Android Java Spinner for United States with postal abbreviations

For the Android version of Monster Cache, I wanted a spinner on my registration page that showed all of the 50 United States.  But when a user chose a state, I wanted to save the US postal abbreviation rather than the state name.  This seems like a common thing people would want to do, but I couldn't find any example, so I thought I would post mine.

First I created two arrays -- one with state names and one with state abbreviations.  Then I loaded the array of names into the spinner using an ArrayAdapter.


                stateCodes = new String[52];  // 50 states + DC + blank
                stateNames = new String[52];

                // set first item to blank
                int stateCount = 0;
                stateCodes[stateCount ] = "";
                stateNames[stateCount ++] = "";

            // now add all US states
            stateCodes[stateCount]= "AL";
            stateNames[stateCount++] = "Alabama";
            stateCodes[stateCount]= "AK";
            stateNames[stateCount++] = "Alaska";
            stateCodes[stateCount]= "AZ";
            stateNames[stateCount++] = "Arizona";
            stateCodes[stateCount]= "AR";
            stateNames[stateCount++] = "Arkansas";
            stateCodes[stateCount]= "CA";
            stateNames[stateCount++] = "California";
            stateCodes[stateCount]= "CO";
            stateNames[stateCount++] = "Colorado";
            stateCodes[stateCount]= "CT";
            stateNames[stateCount++] = "Connecticut";
            stateCodes[stateCount]= "DE";
            stateNames[stateCount++] = "Delaware";
            stateCodes[stateCount]= "FL";
            stateNames[stateCount++] = "Florida";
            stateCodes[stateCount]= "GA";
            stateNames[stateCount++] = "Georgia";
            stateCodes[stateCount]= "HI";
            stateNames[stateCount++] = "Hawaii";
            stateCodes[stateCount]= "ID";
            stateNames[stateCount++] = "Idaho";
            stateCodes[stateCount]= "IA";
            stateNames[stateCount++] = "Iowa";
            stateCodes[stateCount]= "IL";
            stateNames[stateCount++] = "Illinois";
            stateCodes[stateCount]= "IN";
            stateNames[stateCount++] = "Indiana";
            stateCodes[stateCount]= "KS";
            stateNames[stateCount++] = "Kansas";
            stateCodes[stateCount]= "KY";
            stateNames[stateCount++] = "Kentucky";
            stateCodes[stateCount]= "LA";
            stateNames[stateCount++] = "Louisiana";
            stateCodes[stateCount]= "ME";
            stateNames[stateCount++] = "Maine";
            stateCodes[stateCount]= "MD";
            stateNames[stateCount++] = "Maryland";
            stateCodes[stateCount]= "MA";
            stateNames[stateCount++] = "Massachusetts";
            stateCodes[stateCount]= "MI";
            stateNames[stateCount++] = "Michigan";
            stateCodes[stateCount]= "MN";
            stateNames[stateCount++] = "Minnesota";
            stateCodes[stateCount]= "MS";
            stateNames[stateCount++] = "Mississippi";
            stateCodes[stateCount]= "MO";
            stateNames[stateCount++] = "Missouri";
            stateCodes[stateCount]= "MT";
            stateNames[stateCount++] = "Montana";
            stateCodes[stateCount]= "NE";
            stateNames[stateCount++] = "Nebraska";
            stateCodes[stateCount]= "NV";
            stateNames[stateCount++] = "Nevada";
            stateCodes[stateCount]= "NH";
            stateNames[stateCount++] = "New Hampshire";
            stateCodes[stateCount]= "NJ";
            stateNames[stateCount++] = "New Jersey";
            stateCodes[stateCount]= "NM";
            stateNames[stateCount++] = "New Mexico";
            stateCodes[stateCount]= "NY";
            stateNames[stateCount++] = "New York";
            stateCodes[stateCount]= "NC";
            stateNames[stateCount++] = "North Carolina";
            stateCodes[stateCount]= "ND";
            stateNames[stateCount++] = "North Dakota";
            stateCodes[stateCount]= "OH";
            stateNames[stateCount++] = "Ohio";
            stateCodes[stateCount]= "OK";
            stateNames[stateCount++] = "Oklahoma";
            stateCodes[stateCount]= "OR";
            stateNames[stateCount++] = "Oregon";
            stateCodes[stateCount]= "PA";
            stateNames[stateCount++] = "Pennsylvania";
            stateCodes[stateCount]= "RI";
            stateNames[stateCount++] = "Rhode Island";
            stateCodes[stateCount]= "SC";
            stateNames[stateCount++] = "South Carolina";
            stateCodes[stateCount]= "SD";
            stateNames[stateCount++] = "South Dakota";
            stateCodes[stateCount]= "TN";
            stateNames[stateCount++] = "Tennessee";
            stateCodes[stateCount]= "TX";
            stateNames[stateCount++] = "Texas";
            stateCodes[stateCount]= "UT";
            stateNames[stateCount++] = "Utah";
            stateCodes[stateCount]= "VT";
            stateNames[stateCount++] = "Vermont";
            stateCodes[stateCount]= "VA";
            stateNames[stateCount++] = "Virginia";
            stateCodes[stateCount]= "WA";
            stateNames[stateCount++] = "Washington";
            stateCodes[stateCount]= "DC";
            stateNames[stateCount++] = "Washington DC";
            stateCodes[stateCount]= "WV";
            stateNames[stateCount++] = "West Virgina";
            stateCodes[stateCount]= "WI";
            stateNames[stateCount++] = "Wisconsin";
            stateCodes[stateCount]= "WY";
            stateNames[stateCount++] = "Wyoming";

            Spinner dropdown = (Spinner)findViewById(R.id.register_state);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(m_thisActivity, android.R.layout.simple_spinner_item, stateNames);
            dropdown.setAdapter(adapter);


When I process the registration form, I just index the stateCodes array using the index selected in the Spinner, like this:

                Spinner states = (Spinner)findViewById(R.id.register_state);
                String selectedState = stateCodes[states.getSelectedItemPosition()];