I haven’t found any good code snippet for Phone EditBox with nice country flags and validation. So I created my own version.

What I am talking about:

Almost any serious project now has phone verification activity – it sends SMS code to the phone number and verifies the phone. What this activity should do:

  • Validate phone number
  • Automatically select country based on phone network or Line1 number
  • Do not delete “+” before phone number
  • Detect country name and draw nice country flag image
  • Some countries has the same country code (like US and Canada) – good editbox shall detect this

Android Phone EditBox with validation and country flag

  1. Phone validation could be easily implemented with 'com.googlecode.libphonenumber:libphonenumber:6.2' library. This awesome lib can do almost anything.
  2. Then we need country list. I took the country list from here, modified it and packed to assets/countries.dat file.
  3. I do not remember the exact web site where I got flag pictures – but it is quite simple to write a script to download all 232 flag pictures, format it to one size and export to xxhdpi png. If you need original PSD, download it here.
  4. One tricky part – do not delete “+” before phone number. I took orifginal PhoneNumberFormattingTextWatcher from android source and modified it to do so.

Source code is available here https://github.com/dlukashev/android-phone-number-with-flags – feel free to use it in your project.