CountryPicker is a simple fragment that can be embedded or shown as dialog. See the example to see more detail.
The functions are simple:
-
Allow user to search the country
-
Inform client which country user has selected
-
Convenient function to get currency code of the selected country
Define jitpack.io as a repository
repositories {
// ...
maven { url 'https://jitpack.io' }
// ...
}
Include country picker
dependencies {
// ...
compile 'com.github.SocialbitGmbH:AndroidCountryPicker:1.0.3@aar'
// ...
}
Note the library requires 'com.android.support:support-v4'
If you want to use the country auto-selection based on the user locatiion you must put this permission in your app manifest:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
To use CountryPicker in your code as a fragment:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//The second parameter determines if the country must be auto-selected from the user current location.
CountryPicker picker = CountryPicker.newInstance(null, true);
transaction.replace(R.id.home, picker);
transaction.commit();
To show CountryPicker as a dialog:
//The second parameter determines if the country must be auto-selected from the user current location.
CountryPicker picker = CountryPicker.newInstance("Select Country", true);
picker.show(getSupportFragmentManager(), "COUNTRY_PICKER");
When user selects a country, client can listen to that event:
picker.setListener(new CountryPickerListener() {
@Override
public void onSelectCountry(String name, String code) {
// Invoke your function here
}
});
To receive the data that the picker uses:
CountryProvider countryProvider = new CountryProvider(context);
Country de = countryProvider.getCountryByCode("ES"); // ignores case
Log.d(de.getName()) // Spain
List<Country> countries = countryProvider.getCountries();
The data is from CountryPicker by nicklockwood (https://github.com/nicklockwood/CountryPicker)
I converted his data in "Countries.plist" to json format to avoid extra dependency.
Thanks Nick for his awesome library!
Thanks to Javi for the Spanish translation (https://github.com/Mun0n/AndroidCountryPicker)
See LICENSE.md