Reputation: 3262
If I want to authenticate my users via a phone number, what is the best way to add a drop down of all countries with their country code, so that they won't have to manually write "+xxx"?
Upvotes: 0
Views: 3809
Reputation: 527
This problem can be easily handled using a third-party library. Add Country Code Picker (CCP)
library to your project and let it handle the country code things for you. For this what you have to do :
Add this to your Gradle file and sync
dependencies {
implementation 'com.hbb20:ccp:2.2.3'
}
NOTE : Check original documentation for any updates of this library.
Add CCP view to XML layout
<com.hbb20.CountryCodePicker
android:id="@+id/ccp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Add CCP object in Activity / Fragment
CountryCodePicker ccp;
Bind CCP from the layout
ccp = findViewById(R.id.ccp);
That's it. Run the project and see the results.
To blend CCP with your project's theme style, you can modify CCP Theme and CCP Dialog Theme.
For customization check out their wiki page.
Upvotes: 6
Reputation: 1126
Do an spinner
and populate it with the list of country codes
Here's a link to an api that retrieves the list for you https://restcountries.eu/rest/v2/all
Upvotes: 0