joielechong / CountryCodePicker

Country Code Picker (CCP) is an android library which provides an easy way to search and select country phone code ( national code ) for the telephone number.
Apache License 2.0
317 stars 145 forks source link

Phone number formatting NOT being updated when selected country changes #53

Closed dleonett closed 5 years ago

dleonett commented 5 years ago

When selecting a new country code, the phone number formatting is not being updated.

After some research into library code, I found out the issue is because even though mPhoneNumberWatcher gets updated with a new instance, it is not being added again to textView as a TextWatcher.

From library code:

private void setPhoneNumberWatcherToTextView(TextView textView, String countryNameCode) {
    if (mIsEnablePhoneNumberWatcher) {
        if (mPhoneNumberWatcher == null) {
            mPhoneNumberWatcher = new PhoneNumberWatcher(countryNameCode);
            textView.addTextChangedListener(mPhoneNumberWatcher);
        } else {
            if (!mPhoneNumberWatcher.getPreviousCountryCode().equalsIgnoreCase(countryNameCode)) {
                mPhoneNumberWatcher = new PhoneNumberWatcher(countryNameCode); // <- HERE
            }
        }
    }
}

Please note that just assigning a new value to mPhoneNumberWatcher won't update the object within textView listeners list.

Please refer to: https://stackoverflow.com/questions/7080546/add-an-object-to-an-arraylist-and-modify-it-later

Possible solution

We should remove previous mPhoneNumberWatcher and then add the new instance using textView.addTextChangedListener(mPhoneNumberWatcher) to properly update the phone number format according to the new selected country.

joielechong commented 5 years ago

Thank you for the issue and the code for fixing the problem! I have updated the library in https://github.com/joielechong/CountryCodePicker/commit/1466aec75b6ad79bf03ad72583de2cb98ae918b9 commit.

Please take a look if the problem have been solved on https://github.com/joielechong/CountryCodePicker/blob/master/release/sample-debug.apk

joielechong commented 5 years ago

And sorry for the late response :(.