I'm currently trying to implement this:
I've already done the spinner part, though the phone EditText
, I'm not so sure about how to make a TextWatcher for it.
I have 2 options:
I use:
phoneNumberET.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
Or, I use
phoneNumberET.addTextChangedListener(new PhoneNumberFormattingTextWatcher("countryCode"));
Both of these have their flaws. In #1, it won't properly format since the country code isn't found in the EditText
, but in the Spinner
.
In #2 I can only use this in API level 21 and above.
So, is there a solution that can let me format the phone number properly with a country code from the spinner and be available from below API 21?
Earlier platform levels don't have an API for phone number formatting in other regions, you'll need to bundle it with your app. You can use a library such as googlei18n/libphonenumber (or a fork like libphonenumber-android) to provide the data and formatting, then add a TextWatcher for integration with Android.