AlmogBaku / IntlPhoneInput

International phone input for Android
Other
164 stars 96 forks source link

setNumber doesn't update the flag #9

Open dmsherazi opened 8 years ago

dmsherazi commented 8 years ago

if I set Number to +975xxxxxx it should update the selected country to UAE but it doesnt

dmsherazi commented 8 years ago

This happens when you are using the the widget in a dialog etc. So the countryFlag must be via a runOnUiThread runnable

lucniner commented 8 years ago

Hi, i had the same problem on android 4.2. For me the following workaround worked: 1) get all countries first CountriesFetcher.CountryList allCountries = CountriesFetcher.getCountries(getApplicationContext());

2) get your dialcode as an integer: int dialCode = Integer.parseInteger(phoneNumber.substring(1, 3));

3) get the index of the dialcode in the allcountriesList int indexOfCDialCode = allCountries.indexOfDialCode(dialCode);

4) get the iso2 name String iso2Countryname = allCountries.get(indexOfCDialCode).getIso();

5) use the set default method of the phoneInputView phoneInputView.setEmptyDefault(iso2Countryname);

Hope this works for you.

AlmogBaku commented 7 years ago

@dmsherazi have you figure this out? is that required a change in the code? or using the method on your end under the UI Thread?

dmsherazi commented 7 years ago

@AlmogBaku I faced this problem when I was using it in an AlertDialog and using the setNumber method to fill in the number from contact picker result. For me I made the change in the library code.

AlmogBaku commented 7 years ago

what did you changed?

Joakim432710 commented 7 years ago

The problem is that

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

Runs when the text changes, and because

/**
     * Set Number
     *
     * @param number E.164 format or national format(for selected country)
     */
    public void setNumber(String number) {
        try {
            String iso = null;
            if (mSelectedCountry != null) {
                iso = mSelectedCountry.getIso();
            }
            Phonenumber.PhoneNumber phoneNumber = mPhoneUtil.parse(number, iso);

            int countryIdx = mCountries.indexOfIso(mPhoneUtil.getRegionCodeForNumber(phoneNumber));
            mCountrySpinner.setSelection(countryIdx);

            mPhoneEdit.setText(mPhoneUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.NATIONAL));
        } catch (NumberParseException ignored) {
        }
    }

Is intended to use the currently selected country. I have only tested this under my own local usage but it seems to be intended behaviour:

/**
     * Set Number
     *
     * @param number E.164 format or national format
     */
    public void setNumber(String number) {
        try {
            String iso = null;
            if (mSelectedCountry != null) {
                iso = mSelectedCountry.getIso();
            }
            Phonenumber.PhoneNumber phoneNumber = mPhoneUtil.parse(number, iso);

            int countryIdx = mCountries.indexOfIso(mPhoneUtil.getRegionCodeForNumber(phoneNumber));
        mSelectedCountry = mCountries.get(countryIdx);      
            mCountrySpinner.setSelection(countryIdx);

            mPhoneEdit.setText(mPhoneUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.NATIONAL));
        } catch (NumberParseException ignored) {
        }
    }

I have created a pull request if you wish to import or test, see: https://github.com/AlmogBaku/IntlPhoneInput/pull/18

dmsherazi commented 7 years ago

Thanks @Joakim432710 I would test it ...

patrickmuhi commented 7 years ago

I had the same issues, i simply edited the setNumber method, replacing NATIONAL with E.164, this automatically sets the flags!

/**
     * Set Number
     *
     * @param number E.164 format or national format
     */
    public void setNumber(String number) {
        try {
            String iso = null;
            if (mSelectedCountry != null) {
                iso = mSelectedCountry.getIso();
            }
            Phonenumber.PhoneNumber phoneNumber = mPhoneUtil.parse(number, iso);

            int countryIdx = mCountries.indexOfIso(mPhoneUtil.getRegionCodeForNumber(phoneNumber));
        mSelectedCountry = mCountries.get(countryIdx);      
            mCountrySpinner.setSelection(countryIdx);

            mPhoneEdit.setText(mPhoneUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.E164));
        } catch (NumberParseException ignored) {
        }
    }
AlmogBaku commented 7 years ago

I think that fixed by #18 ?

aris0 commented 7 years ago

The issue is still there, setNumber doesn't update the flag