Search code examples
androidlibphonenumberandroid-textwatcher

How to get libphonenumber to format French phone numbers?


I am looking into the libphonenumber library (https://github.com/googlei18n/libphonenumber), but I am only able to get it to work for "US" and "BR" regions. How do I get it to work for "FR" region? The format I am seeking is 1 41 02 25 00.

I was able to get this format with my own code

            @Override
            public void afterTextChanged(Editable s) {
                // phone digits without formatting
                phone = s.toString().replaceAll("[^\\d]", "");
                        if (phone.length() == 1) {
                            edtPhoneNumber.setText(s.toString().concat(" "));
                            // move cursor to original position relative to the end of the string
                            edtPhoneNumber.setSelection(edtPhoneNumber.getText().length() - cursorPos);
                        } else {
                            if (pairings.length() >= 2) {
                                pairings = "";
                            }
                            pairings = pairings.concat(phone.substring((phone.length()-1)));
                            if (pairings.length() >= 2) {
                                if (phone.length() < 9) {
                                    edtPhoneNumber.setText(s.toString().concat(" "));
                                } else {
                                    edtPhoneNumber.setText(s.toString());
                                }
                            } else {
                                edtPhoneNumber.setText(s.toString());
                            }
                            // move cursor to original position relative to the end of the string
                            edtPhoneNumber.setSelection(edtPhoneNumber.getText().length() - cursorPos);
                        }
              }

My implementation of the library is as follows. After instantiating with the region code of interest,

AsYouTypeFormatter aytf = PhoneNumberUtil.getInstance().getAsYouTypeFormatter("FR") 

I then have the following code inside of afterTextChanged(Editable s)

                    if(phone.length() > 0){
                        for(int i = 0; i < phone.length(); i++){
                            formattedPhoneNumber = aytf.inputDigit(phone.charAt(i));

                        }
                        //The formatted output shows properly in this EditText but not when I try to put it back into the original one (phoneNumberText)
                        edtPhoneNumber.setText(formattedPhoneNumber);
                        edtPhoneNumber.setSelection(edtPhoneNumber.getText().length() - cursorPos);
                        aytf.clear();
                    }

                    formattedPhoneNumber = null;
                    isPhoneFormatting = false;

Solution

  • I might be wrong, but according to wiki, the number format for French numbers starts with "0". So your test input is not valid for LibPhoneNumber. https://en.wikipedia.org/wiki/Telephone_numbers_in_France

    enter image description here

    The plan uses a ten-digit closed numbering scheme, where the first two digits denote the area:

    01 Île-de-France
    02 Northwest France
    03 Northeast France
    04 Southeast France
    05 Southwest France
    06 and 07 Mobile phone services
    08 Freephone (numéro vert) and shared-cost services.
    09 Non-geographic number (used by Voice over IP services, formerly 087 numbers)

    If you add "0" in front of your test value, it seems to format it correctly.