1

I would like to display a spinner in android in which the user will select a country? I know how to do the spinner but I am wondering about the the country part. Is there some sort of pre-defined list somewhere in android (or lib) beside the fact that I have to type them manually?

Thanks

Snake
  • 14,228
  • 27
  • 117
  • 250
  • 5
    possible duplicate of [Retrieve a list of countries from the android OS](http://stackoverflow.com/questions/9760341/retrieve-a-list-of-countries-from-the-android-os) – hanleyhansen Mar 01 '14 at 22:32
  • Perfect . Weird it didnt come in my search. I can accept the asnwer if posted – Snake Mar 01 '14 at 22:46

2 Answers2

3

Try this to list all countries:

 Locale[] locales = Locale.getAvailableLocales();
        ArrayList<String> countries = new ArrayList<String>();
        for (Locale locale : locales) {
            String country = locale.getDisplayCountry();
            if (country.trim().length() > 0 && !countries.contains(country)) {
                countries.add(country);
            }
        }

        Collections.sort(countries);
        for (String country : countries) {
            System.out.println(country);
        }

        ArrayAdapter<String> countryAdapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_spinner_item, countries);

countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     // Apply the adapter to the your spinner
            yourcountrySpinnerobj.setAdapter(countryAdapter);

or

Use Geonames client libraries

or

Use geonames webservice

first create your account on Geonames and then use that username

pathe.kiran
  • 2,444
  • 1
  • 21
  • 27
1

Though the question asked way before and the accepted answer works well. I would like to answer the subpart of the question that asks for some library.

The CCP library gives an easy way this task along with the flag of the country.. It allows you to make something like this really easily.

And gives powerful search feature for selection:

1. How to use CCP for country selection?

2. Github Repo of CCP

HBB20
  • 2,743
  • 1
  • 24
  • 35