Country-Picker
Android library to get country data (eg. ISD code, country code, name) from list of countries.
Download
Add this to your project's build.gradle
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
And add this to your module's build.gradle
dependencies {
implementation 'com.github.maayyaannkk:CountryPicker:x.y.z'
}
Usage
Embed layout in xml like
<in.mayanknagwanshi.countrypicker.custom.CountryPickerView
android:id="@+id/countryPickerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:showSearch="true" /><!-- false to hide search edit text -->
and reference in activity/fragment to get selection
CountryPickerView countryPickerView = findViewById(R.id.countryPickerView);
countryPickerView.setCountrySelectListener(countryData -> {
//country details
String countryName = countryData.getCountryName();
String isdCode = countryData.getCountryISD();
String countryCode = countryData.getCountryCode();
});
OR
countryPickerView.getSelectedCountry(); //returns null if nothing was selected
set selection
countryPickerView.setSelectedCountry(new CountryData("IN"));
Option to start the activity as dialog
Intent intent = new Intent(this, CountrySelectActivity.class);
intent.putExtra(CountrySelectActivity.EXTRA_SELECTED_COUNTRY, new CountryData("IN"));//for default selection
startActivityForResult(intent, 1213);
Receive result
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1213 && resultCode == Activity.RESULT_OK) {
CountryData countryData = (CountryData) data.getSerializableExtra(CountrySelectActivity.RESULT_COUNTRY_DATA);
}
}