hbb20 / CountryCodePickerProject

Country Code Picker (CCP) is an android library which provides an easy way to search and select country or international phone code. Also supports Android EditText phone mask and international phone validation.
Apache License 2.0
1.52k stars 506 forks source link

How to use with databinding #250

Closed IEnoobong closed 6 years ago

IEnoobong commented 6 years ago

Please how can I use with databinding to get selectedCountryCodeWithPlus

Thanks!

pankajnegii commented 6 years ago

-In your xml, inside <com.hbb20.CountryCodePicker ...... bind:countryValue="@{viewmodel.code}" />

-In your adapter class, do something like this.

@BindingAdapter("bind:countryValue") public static void setCountryValue(final CountryCodePicker picker, ObservableField code) { picker.setOnCountryChangeListener(new CountryCodePicker.OnCountryChangeListener() { @Override public void onCountrySelected() { code.set(picker.getSelectedCountryCodeWithPlus().toString()); } });

-Inside Viewmodel class,

           public final ObservableField<String> code = new ObservableField<>();

Now, You can get the values using- code.get();

hbb20 commented 6 years ago

@pankajnegii, thank you for the answer. @IEnoobong , we hope @pankajnegii's answer worked for you. If you have any further question feel free to comment back.

IEnoobong commented 6 years ago

Thank you very much @pankajnegii and @hbb20 ! Much appreciated!

Makinul commented 5 years ago

Can anyone help me with LiveData instead of ObservableField.

phixtalk commented 4 years ago

Hello hbb20, thanks for this wonderful library. I've been able to implement it inside of a fragment using android databinding, however i'm finding it a bit of a challenge getting the country code inside of a viewmodel (MVVM) with databinding Would really appreciate any help. Thanks