A user-friendly country picker for Android that allows users to select countries and search using country names, country code names, and country phone codes.
Integrating the Country Picker Android
library into your Android project is simple and
straightforward.
Follow the steps below based on your Gradle setup.
-
Add JitPack to Your Repositories
To use the Country Picker Android library, ensure you include the JitPack repository in your build.gradle files. For Gradle Version 7.0 and Above (Using Dependency Resolution Management) In your project's settings.gradle (or settings.gradle.kts for Kotlin), add the following:
dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { mavenCentral() // groovy maven { url 'https://jitpack.io' } // kotlin maven("https://jitpack.io") } }
For Gradle Version Below 7.0 In your project-level build.gradle (or build.gradle.kts for Kotlin), add JitPack to the repositories section:
allprojects { repositories { google() mavenCentral() // groovy maven { url 'https://jitpack.io' } // kotlin maven("https://jitpack.io") } }
-
Add the Dependency Next, add the CountryPicker library dependency in your app-level build.gradle (or build.gradle.kts for Kotlin):
dependencies { // ...others dependency // groovy implementation 'com.github.waffiqaziz:country-picker-android:Tag' // kotlin implementation("com.github.waffiqaziz:country-picker-android:Tag") }
change
Tag
with the latest version -
Sync Your Project
After adding the dependency, sync your project with Gradle files. You can do this by clicking on the "Sync Now" prompt that appears or by going to File > Sync Project with Gradle Files.
-
Usage Example
The
CountryPicker
library is integrated using XML layouts.Basic implementation
<com.waffiq.CountryPickerButton android:id="@+id/cpb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:clickable="true" android:focusable="true" android:foreground="?attr/selectableItemBackgroundBorderless" app:cardBackgroundColor="@android:color/transparent" app:cardCornerRadius="15dp" app:cardElevation="0dp" app:strokeWidth="0dp" />
CountryPickerButton CountryPickerDialog With customization
<com.waffiq.CountryPickerButton android:id="@+id/cpb_custom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:clickable="true" android:focusable="true" android:foreground="?attr/selectableItemBackgroundBorderless" app:cardBackgroundColor="@color/darkPurple" app:cardCornerRadius="15dp" app:cardElevation="0dp" app:cpa_defaultCountryISO="US" app:cpa_dialogApplyBorderSearch="true" app:cpa_dialogAutoFocus="true" app:cpa_dialogBackgroundColor="@color/darkPurple" app:cpa_dialogSearchBackgroundColor="@color/darkPurple" app:cpa_dialogSearchCrossIconColor="@color/gray" app:cpa_dialogTextColor="@color/gray" app:cpa_dialogTextHintColor="@color/dark_gray" app:cpa_fontFamily="@font/nunito_sans_regular" app:strokeWidth="0dp" />
CountryPickerButton CountryPickerDialog
To set the default country programmatically, you can use the setCountry()
method:
binding.cpb.setCountry("MY") // use viewbinding
// or
findViewById<CountryPickerButton>(R.id.cpb).setCountry("MY")
To get the selected country when using CountryPickerButton
, you can implement
the onCountrySelectedListener
:
binding.cpb.onCountrySelectedListener = {
Toast.makeText(this@MainActivity, it.name, Toast.LENGTH_SHORT).show()
}
To get the selected country without listener using selectedCountryCode
binding.cpb.selectedCountryCode // return "ID" (default country)
Note : You can also explore more about CountryPickerButton
via activity_layout. CountryPickerButton
basically implemented as an extension of MaterialCardView
, giving you more flexibility to
customize its appearance and behavior according to the Material Design guidelines. You can read more
about
MaterialCardView here.
Read this page
If you encounter any issues or bugs, feel free to open a new issue in the repository and provide as
much detail as possible. Include steps to reproduce the issue
, the expected behavior
, and any
relevant logs
.
Grateful acknowledgment to the following resources and projects for their valuable contributions:
- Flagpedia.net - For providing the country flags used in this project.
- CountryCodePickerProject - For inspiring the creation of this project.
Copyright 2024 Waffiq Aziz
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.