Question Detail

Android get all countries on array spinner

6 years ago Views 2601 Visit Post Reply

I've searched a lot but the stuff I found was a little confused.

I need to get the android country list and set default the user locale.

For example, I'm registering a user account and I need to insert the country, in that spinner will show all countries but by default will appear my default locale.


Thread Reply

Anonymous

- 6 years ago

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

Spinner country_obj = (Spinner)findViewById(R.id.country_list_spinner_ID);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, countries);
country_obj.setAdapter(adapter);