How to use Country Picker in Android

Posted By Ankita Singh | 10-Apr-2017

CountryCodePicker will help users to search and select a country and retrieve selected country's country name,code,currency and dial code.For supporting international numbers in our app we must have to include library

"compile 'com.hbb20:ccp:1.7.2'"
in your app.

After that add the below given countrypicker in the xml file where you want to add this in UI.

<com.hbb20.CountryCodePicker
    android:id="@+id/ccp"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.7"
    android:gravity="center"
    app:countryPreference="US,IN,NZ"
    app:defaultCode="91" />

after this do the findviewbyid in the activity class and then add the following code to the Java class file and you will easily get the country code -

String countryCodeAndroid = "91";
CountryCodePicker ccp;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.enter_num_activity);
    ccp.setOnCountryChangeListener(new CountryCodePicker.OnCountryChangeListener() {
        @Override
        public void onCountrySelected() {
            countryCodeAndroid = ccp.getSelectedCountryCode();
            Log.d("Country Code", countryCodeAndroid);
        }
    });
}

after getting country code use it according to yourself.

 

The local file uses a json file of the country codes and parse it to get the arraylist of the country code with dial code and country names.This library is very god because it contain the search option user can easily search the country name just by typing the name in the edittext .

Request for Proposal

Recaptcha is required.

Sending message..