CountryPicker

Additional

Language
Java
Version
1.0.3 (Jun 25, 2019)
Created
Mar 29, 2019
Updated
Jun 25, 2019
Owner
Mayank Nagwanshi (maayyaannkk)
Contributor
Mayank Nagwanshi (maayyaannkk)
1
Activity
Badge
Generate
Download
Source code
APK file

Commercial

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'
}

change x.y.z to version in

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

Option to start the activity as dialog

Intent intent = new Intent(this, CountrySelectActivity.class);
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);           
        }
    }