Skip to content

Basic Usage

Mukesh Solanki edited this page Oct 31, 2019 · 7 revisions

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

  • getCountryFromSIM()
    • 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.