10

Is this possible to load a list of all countries in a spinner, then on selecting the particular country, all of its cities appears in another spinner and after selecting the city, we get the list of areas or towns of that city in android using google-api or something like country picker? Currently i am using this to get the list of countries. but how to get their respective cities and towns?

    ArrayList<String> list=new ArrayList<String>();
String[] locales = Locale.getISOCountries();
for (String countryCode : locales) {
    Locale obj = new Locale("", countryCode);
    list.add(obj.getDisplayCountry());
}
Nizami
  • 227
  • 1
  • 5
  • 15

1 Answers1

9

You can use the JSON file available here. Just add it to your project into the assets folder and add the list of cities and countries to spinner after parsing it.

Jordan
  • 713
  • 15
  • 30
  • How do you parse this thing? – f.trajkovski Feb 29 '20 at 23:24
  • check this out for details about JSON parsing https://stackoverflow.com/questions/9605913/how-do-i-parse-json-in-android – Jordan Mar 01 '20 at 11:54
  • 1
    new Gson().fromJson(getContentFromCountryFile(), new TypeToken>>() { }.getType()) If someone else is trying to solve it. Just put this dependency in the gradle: implementation 'com.google.code.gson:gson:2.8.5' – f.trajkovski Mar 01 '20 at 13:38