How to add a Country Picker for Android in few lines?

Mukesh Solanki
4 min readSep 1, 2018

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

Getting Started

Integrating and using the project is simple all you need to do is follow the below step

  • 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" }
}
}
  • Add the dependency
dependencies {
compile 'com.github.mukeshsolanki:country-picker-android:<latest-version>'
}

Basic Usage

Once the project has been added to gradle, You can use the builder to configure and display it as a dialog or as a bottom sheet.

The country picker library is built using the builder pattern to help you easily customize and only specify the options that you want to configure. A basic example to use the builder would be likewise.

CountryPicker.Builder builder =
new CountryPicker.Builder().with(Context)
.listener(OnCountryPickerListener);

The OnCountryPickerListener is used to provide the country picked in the dialog or the bottom sheet. you can simply keep adding the customization options you would like to edit to the builder :)

Dialog

To display a Dialog you first need to create the builder as mentioned above after that all you need to do is build it and call showDialog() to show the country picker dialog. Below is the sample snippet assuming you have already got a builder object.

CountryPicker picker = builder.build();picker.showDialog(Activity)

That’s it and you should see the country picker dialog popup.

Bottom sheet

The process to create the bottom sheet is similar to that of the creating a dialog with the only exception being is calling showBottomSheet(). Below is the sample snippet assuming you have already got a builder object.

CountryPicker picker = builder.build();picker.showBottomSheet(Activity)

That’s it and you should see the beautiful bottom sheet slide up.

Utility methods

Apart from these methods the CountryPicker also provides some additional methods that you could use

  • getAllCountries(): This method will get you a list of all the countries available.
  • getCountryFromSIM(Context): This method will get the country based on the sim card in the device.
  • getCountryByLocale(Locale): This method will get the country based on the locale provided.
  • getCountryByName(String): This method will return the country which matches the name provided.
  • getCountryByISO(String): This method will return the country based on the ISO code provided.

Sorting

Country Picker allows you to change the order of the counties listed. To sort the list you need to provide the sort type to the builder by calling sortBy(). Below is a snippet of how to apply sort.

CountryPicker.Builder builder =
new CountryPicker.Builder().with(Context)
.listener(OnCountryPickerListener).sortBy(SORT_TYPE);

We can sort the library in 3 different ways.

Sort by Name

As the name suggests this will sort the list in alphabetic order. To apply this sorting technique pass CountryPicker.SORT_BY_NAME to sortBy() in the builder.

Sort by ISO

As the name suggests this will sort the list by ISO. To apply this sorting technique pass CountryPicker.SORT_BY_ISO to sortBy() in the builder.

Sort by Dial Code

As the name suggests this will sort the list by Dial Code. To apply this sorting technique pass CountryPicker.SORT_BY_DIAL_CODE to sortBy() in the builder.

Searching

The library already provides the mechanism for searching and is enabled by default. To toggle the searching you need to call canSearch() on the builder. Below is a code snippet of how you can toggle it.

CountryPicker.Builder builder =
new CountryPicker.Builder().with(Context)
.listener(OnCountryPickerListener).canSearch(false);

You can even change the search icon in the library and replace it will a custom icon. You can learn more about it in Styling

Styling

One of the key things that separate this library from the rest is the ability to customise this view to match your app style with easy.

Creating the style

It’s very simple to create a style for the library it is the same as it would have been while writing styles for default android widgets. To create a style open the styles.xml and a new style to it which we will later be applied to the library. Currently, only a few attributes are accepted. Below is an example of the style.

<style name="CountryPickerStyle">
<item name="android:textColor">@android:color/white</item>
<item name="android:textColorHint">@android:color/white</item>
<item name="android:background">#424242</item>
<item name="android:drawable">@drawable/ic_dummy_icon</item>
</style>
  • android:textColor: This property will be used to set the primary colour of the text of the search EditText and the list items.
  • android:textColorHint: This property will be used to set the hint colour of the EditText and the tint of the search icon.
  • android:background: This property will be used to set the background colour of the Dialog or the BottomSheet.
  • android:drawable: This property will be used to replace the default search icon with any custom icon of your choice.

Applying the style

To apply a custom style to the library all you need to do is call style() on the builder and pass the style defined in styles.xml. Below is a snippet of how to apply the style.

CountryPicker.Builder builder =
new CountryPicker.Builder().with(Context)
.listener(OnCountryPickerListener).style(R.style.CountryPickerStyle);

Themeing

The library also provides an option to use the new Material Design 2 look and feel with the rounded corners. This is turned on by default. But if you want to turn it off it can be done with one single switch and it will switch back to the old boxier look. All you need to do is call theme() on the builder. It accepts only CountryPicker.THEME_NEW and CountryPicker.THEME_OLD. Below is the snippet on how to toggle this.

CountryPicker.Builder builder =
new CountryPicker.Builder().with(Context)
.listener(OnCountryPickerListener).theme(CountryPicker.THEME_OLD);

--

--

Mukesh Solanki

Converts coffee to code! Writes about software development, automation, android. Connect with me on https://www.mukeshsolanki.com