pileup
pileup

Reputation: 3262

How to create a dropdown list of country codes for phone authentication?

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

Answers (2)

Dipayan Ray
Dipayan Ray

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 :

  1. 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.

  2. Add CCP view to XML layout

    <com.hbb20.CountryCodePicker
        android:id="@+id/ccp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    
  3. Add CCP object in Activity / Fragment

    CountryCodePicker ccp;
    
  4. Bind CCP from the layout

    ccp = findViewById(R.id.ccp);
    
  5. That's it. Run the project and see the results.

  6. 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

Joaqu&#237;n
Joaqu&#237;n

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

Related Questions