Android CountryCodePicker set country to null

2.2k Views Asked by At

I use the CountryCodePicker from the CountryCodePickerProject by hbb20.

I get the User's data from the DataBase, and sometimes for whatever reason the User has "" (empty string) saved as their country.

I use this Picker to edit their country, and if the User does not change his country then I get India "IN" as the selectedCCPCountry since the defaultCCPCountry automatically returns India if no country was set.

How can I set the selectedCCPCountry or some other flag to null or "" so that in this case I can know not to change the User's data?

Thanks!

1

There are 1 best solutions below

0
Avramo On

So I created an issue for this on GitHub and got a quick response from the author:

It seems like your primary use case is to select country without phone number validation, did I get that right? if so, you should try Android Country Picker (https://github.com/hbb20/AndroidCountryPicker). It has out of box support for non selection and much more. Please let me know if that works for you.

Here is my response and my temporary workaround:

As I don't want to undo all the code I have already, I made a temporary workaround. mCountryChangedByUser is false and turns true only when a country is selected inside the CountryCodePicker.

mCountryCCP.setOnCountryChangeListener(new CountryCodePicker.OnCountryChangeListener() {
    @Override
    public void onCountrySelected() {
        mCountryChangedByUser = true;
        mCountryTextTV.setText(mCountryCCP.getSelectedCountryName());
        mUser.setCountry(mCountryCCP.getSelectedCountryNameCode());
    }
});

If the user did not change his country then just return the original country in my saveUser() function.

if (mCountryChangedByUser) {

    userUpdate.setCountry(mCountryCCP.getSelectedCountryNameCode() );  

} else {  
    userUpdate.setCountry(mUser.getCountry());  
}

Maybe next time I'll use the AndroidCountryPicker.

Thanks!