EditText phone number format text watcher but with different country codes from a spinner on pre-Lollipop

1.7k views Asked by At

I'm currently trying to implement this:

image

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:

  1. I use:

    phoneNumberET.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

  2. 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?

1

There are 1 answers

0
ephemient On

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.