Skip to content

A simple library that displays a beautiful list of all the countries allowing the user to pick the country he wishes and provide details like country code, iso code name and flag.

License

kykrueger/country-picker-android

 
 

Repository files navigation

Country Picker for Android



CountryPicker is a simple library that can be show a country picker. See the example to see more detail.

How to use

Integrating the project is simple a refined all you need to do is follow the below steps

Step 1. Add the JitPack repository to your build file. Add it in your root build.gradle at the end of repositories:

allprojects {
  repositories {
    ...
    maven { url "https://jitpack.io" }
  }
}

Step 2. Add the dependency

dependencies {
        compile 'com.github.mukeshsolanki:country-picker-android:1.1.6'
}

Once the project has been added to gradle the user can implement this with easy.

CountryPicker picker = CountryPicker.newInstance("Select Country");
picker.show(getSupportFragmentManager(), "COUNTRY_PICKER");
picker.setListener(new CountryPickerListener() {
    @Override
    public void onSelectCountry(String name, String code, String dialCode, int flagDrawableResID) {
        // Implement your code here
    }
});

That's it your all done.

Get user country based on sim

The following code will get the current users country details based on sim.

CountryPicker picker = CountryPicker.newInstance("Select Country");
Country country = picker.getUserCountryInfo(this);
//TODO use the country object

Get country from english name

  public Country getCountryByName ( Context context, String countryName ) {
    this.context = context;
    Map<String, String> countries = new HashMap<>();
    for (String iso : Locale.getISOCountries()) {
      Locale l = new Locale("", iso);
      countries.put(l.getDisplayCountry(), iso);
    }

    String countryIsoCode = countries.get(countryName);
    if (countryIsoCode != null) {
      return getCountry(countryIsoCode);
    }
    return afghanistan();
  }

After you have initially selected a country, you may want to store it and retrieve the flag or other details later. This method allows you to store the selected country by its name, and later retrieve it with that same name. Try it as follows

CountryPicker picker = CountryPicker.newInstance("Select Country");
Country country = picker.getCountryByName("Canada");
//TODO use the country object

Get country from locale

  public Country getCountryByLocale( Context context, Locale locale ) {
    this.context = context;
    String countryIsoCode = locale.getISO3Country().substring(0,2).toLowerCase();
    return getCountry(countryIsoCode);
  }

The Android Locale class uses ISO country name standards. This is useful for storing and then retriving the country that was first selected using the country picker. Pass your desired locale into this method to retrieve its country class and contained flag. Try it as follows

CountryPicker picker = CountryPicker.newInstance("Select Country");
Country country = picker.getCountryByLocale(Locale.GERMANY);
//TODO use the country object

About

A simple library that displays a beautiful list of all the countries allowing the user to pick the country he wishes and provide details like country code, iso code name and flag.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%