CountryCodePickerProject icon indicating copy to clipboard operation
CountryCodePickerProject copied to clipboard

Not able to set country code programmatically in kotlin

Open harish-entri opened this issue 4 years ago • 2 comments

How to set country coe got from string int ccp ui

harish-entri avatar Apr 17 '21 14:04 harish-entri

I'm having a similar issue on Java. Using setDefaultCountryUsingNameCode("CODE") is not setting the default country as selected. For example, if I set setDefaultCountryUsingNameCode("US") I still see India as the selected country.

Setting app:ccp_defaultNameCode="US" from the xml works, but I need to set it dinamically from code.

Looks like is missing a call to set selectedCCPCountry, not sure if intentional. Also this method is not public so I'm not able to change it from my code.

I was able to make it work by: binding.phoneCountryPicker.setDefaultCountryUsingNameCode(<CountryCode>); and then calling binding.phoneCountryPicker.resetToDefaultCountry();

The only downside is that you get the default India selected for a moment

perez-alex avatar May 04 '21 15:05 perez-alex

Yeah.. resetToDefaultCountry() is by design.

setDefaultCountryUsingNameCode() or setDefaultCountryUsingPhoneCode() will not set default country as selected country in CCP view. To set default country as selected country in CCP view, call resetToDefaultCountry() method. Source wiki

The reason for downside is widget needs to be initiated before it shows up and during initialization xml property can be read. and setDefaultCountryUsingNameCode() is called after it's initialized.

Just FYI: If your primary use-case is county picker and not phone number utilities then you can use kotlin-first library Android Country Picker.

hbb20 avatar May 04 '21 15:05 hbb20