In this tutorial we will look at some of the best Android CountryPicker libraries and how to use them.

1. hbb20/AndroidCountryPicker

An android countrypicker library:

Here is the demo screenshot of this library in use:

Step 1. Add dependency

Add in your app/build.gradle:

         dependencies {
           implementation 'com.hbb20:android-country-picker:X.Y.Z'
          }

Step 2: Add to Layout

For the Default Country Picker View add following to your XML layout:

   <com.hbb20.CountryPickerView
   android:id="@+id/countryPicker"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />

Step 2: Write Code

Modify view / dialog / list config in activity or fragment:

private fun setupCountryPickerView() {
        val countryPicker = findViewById<CountryPickerView>(R.id.countryPicker)

        // Modify CPViewConfig if you need. Access cpViewConfig through cpViewHelper
        countryPicker.cpViewHelper.cpViewConfig.viewTextGenerator = { cpCountry: CPCountry ->
            "${cpCountry.name} (${cpCountry.alpha2})"
        }
        // make sure to refresh view once view configuration is changed
        countryPicker.cpViewHelper.refreshView()

        // Modify CPDialogConfig if you need. Access cpDialogConfig through countryPicker.cpViewHelper
        // countryPicker.cpViewHelper.cpDialogConfig.

        // Modify CPListConfig if you need. Access cpListConfig through countryPicker.cpViewHelper
        // countryPicker.cpViewHelper.cpListConfig.

        // Modify CPRowConfig if you need. Access cpRowConfig through countryPicker.cpViewHelper
        // countryPicker.cpViewHelper.cpRowConfig.
    }

To Launch Country Picker Dialog add following to your Activity/Fragment:

   context.launchCountryPickerDialog { selectedCountry: CPCountry? ->
     // your code to handle selected country
   }

To Load countries in RecyclerView add following to your Activity/Fragment:

   recyclerView.loadCountries { selectedCountry: CPCountry -> 
     // your code to handle selected country
   }

Reference

Read More about Country Picker View and available configuration.

Or:

Read Individually.

2. joielechong/CountryCodePicker

A Country Code Picker Library.

Country Code Picker (CCP) is an android library which provides an easy way to search and select country phone code for the telephone number.

CCP gives professional touch to your well designed form like login screen, sign up screen, edit profile screen.
CCP removes confusion about how to add number and thus make view more understandable. Finally reduces mistakes in user input.

The most recommended usage for CCP is using the default setting so the library will auto check the all the value.
To do that, you need to follow the following steps:

  1. Add CCP view to layout
  2. Add EditText view to layout
  3. register the EditText using registerPhoneNumberTextView(editText) we can also use TextView instead of editText.
  4. Let the magic happens 😉

Step 1: Install it

How to add to your project

  1. Add jitpack.io to your root build.gradle file:
    allprojects {
        repositories {
            jcenter()
            maven { url "https://jitpack.io" }
        }
    }
  1. Add library to your app build.gradle file then sync
    dependencies {
        implementation 'com.github.joielechong:countrycodepicker:2.4.2'
    }

Step 2: Add to Layout

Add ccp view to xml layout

    <com.rilixtech.widget.countrycodepicker.CountryCodePicker
          android:id="@+id/ccp"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />

If you want to Add EditText view to layout:

     <EditText
            android:id="@+id/phone_number_edt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="phone"
            android:inputType="phone"/>

Step 4: Show in Activity/Fragment:

Add ccp object in Activity / Fragment

    CountryCodePicker ccp;

Then Bind ccp from layout

    ccp = (CountryCodePicker) findViewById(R.id.ccp);

That's it. Run the project and see the results.

If you want to register an EditText with code:

   CountryCodePicker ccp;
   AppCompatEditText edtPhoneNumber;

   ...

   ccp = (CountryCodePicker) findViewById(R.id.ccp);
   edtPhoneNumber = findViewById(R.id.phone_number_edt);

   ...

   ccp.registerPhoneNumberTextView(edtPhoneNumber);

You can check validity of phone number using isValid() method.

Attributes

Here the attributes that can be used in CountryCodePicker layout:

Attribute method Description
ccp_defaultCode setDefaultCountryUsingPhoneCodeAndApply(int defaultCode) set selected Flag and phone in CCP by phone code.
ccp_showFullName showFullName(boolean show) Show full name of country in CCP. Default is false
ccp_hideNameCode hideNameCode(boolean hide) Hide the country name code. Default is false
ccp_hidePhoneCode hidePhoneCode(boolean hide) Hide the phone code. Default is false

Reference

Read more here.

Or:

Read Individually.