joielechong / CountryCodePicker

Country Code Picker (CCP) is an android library which provides an easy way to search and select country phone code ( national code ) for the telephone number.
Apache License 2.0
317 stars 144 forks source link
country phone-number telephone-number

Android Arsenal

Country Code Picker Library

Country Code Picker (CCP) is an android library which provides an easy way to search and select country phone code for the telephone number.

Introduction

  1. register the EditText with code:

    CountryCodePicker ccp;
    AppCompatEditText edtPhoneNumber;
    
    ...
    
    ccp = (CountryCodePicker) findViewById(R.id.ccp);
    edtPhoneNumber = findViewById(R.id.phone_number_edt);
    
    ...
    
    ccp.registerPhoneNumberTextView(edtPhoneNumber);
  2. Now look at the magic ;)

you can check validity of phone number using isValid() method.

How to add to your project

  1. Add jitpack.io to your root build.gradle file:

    allprojects {
        repositories {
            jcenter()
            maven { url "https://jitpack.io" }
        }
    }
  2. Add library to your app build.gradle file then sync

    dependencies {
        implementation 'com.github.joielechong:countrycodepicker:2.4.2'
    }
  3. Add ccp view to xml layout

    <com.rilixtech.widget.countrycodepicker.CountryCodePicker
          android:id="@+id/ccp"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />
  4. Add ccp object in Activity / Fragment

    CountryCodePicker ccp;
  5. Bind ccp from layout

    ccp = (CountryCodePicker) findViewById(R.id.ccp);
  6. That's it. Run the project and see the results.

For DexGuard users

If your project is obfuscated with DexGuard you may need to add the following line to the DexGuard configuration:

  -keepresourcefiles assets/io/michaelrocks/libphonenumber/android/**

This is because this library use libphonenumber-android

Attributes

Here the attributes that can be used in CountryCodePicker layout:

Attribute method Description
ccp_defaultCode setDefaultCountryUsingPhoneCodeAndApply(int defaultCode) set selected Flag and phone in CCP by phone code.
ccp_showFullName showFullName(boolean show) Show full name of country in CCP. Default is false
ccp_hideNameCode hideNameCode(boolean hide) Hide the country name code. Default is false
ccp_hidePhoneCode hidePhoneCode(boolean hide) Hide the phone code. Default is false

TBD.

Features

If you prefer experience along with only reads, an demo android app is available that demonstrates all the features of this library. Click below button to download from Playstore.

If you just want to read them, here you go:

1. Default country

2. Choose and set country

Choosing and setting country will update selected country in CCP view.

Choose Country

  1. In order to choose country, click on CCP view.
  2. Then search country by country name or phone code or name code in dialog.
  3. Click on county from list to choose

    Set country programmatically

    Using country code name
    Country in CCP can be using setCountryForNameCode() method.

    Using phone code

    • Country in CCP can be using setCountryForCode() method.
    • If specified country code / name code does not match with any country, default country will be set in to CCP.

    How to listen change in selection? To get call back when country is changed, you need to add OnCountryChangeListener from code.

    ccp.setOnCountryChangeListener(new CountryCodePicker.OnCountryChangeListener() {
         @Override
         public void onCountrySelected(Country selectedCountry) {
             Toast.makeText(getContext(), "Updated " + selectedCountry.getName(), Toast.LENGTH_SHORT).show();
         }
    });

3. Country preference

4. Read selected country

Country's 3 properties (Country name, phone code and name code) can be read individually.

5. Full number support

Full number is combination of country code and carrier number. for example, if country code is 91 and carrier number is 8866667722 then 918866667722 or +918866667722 is the full number.

6. Custom text color

Color of CCP text can be changed according to different background.

7. Custom background color

CCP background color can be set to any custom color. It will be defaulting to application theme if not set up.

8. Custom textSize

9. Custom arrow size

Size if Down arrow of CCP view can be modified in order to match rest of the view of form.

10. Hide country name code

By default, text of CCP contains country's name code. i.e "(US) +1". Country name code can be removed if required.

11. Custom master country list

12. Custom font

FontFamily of CCP content can be changed in order to match rest of the view of form.

13. National Flag Thumbnail

14. Show Full Country Name

15. Enable / Disable click

Developer can toggle click listener of CCP

16. Hide / Show Phone Code

The phone code can be hide or show if you want. By default the phone code is shown. We don't need to add the attribute to show the phone code.

17. Enable / Disable Auto Formatter

You need to set an EditText for phone number with registerPhoneNumberTextView() to make use of this. Ignore this if you don't. The auto formatter for EditText can be enable/disable by using enablePhoneAutoFormatter attribute. By default, auto formatter is enabled.

18. Enable / Disable Set Country Code By TimeZone

You need to set an EditText for phone number with registerPhoneNumberTextView() to make use of this. Ignore this if you don't. By default, CCP will checking for country code from time zone if no default country code set and no country code is found from device network. The default value is true.

Use ccp_setCountryByTimeZone attribute to change. This attribute is having the least order from defaultNameCode. Here the order CCP use:

  1. defaultNameCode by using ccp_defaultNameCode
  2. Auto detect from device by checking network
  3. time zone from device by using ccp_setCountryByTimeZone
  4. country iso from device locale.
  5. the last default will be defaulting to country code ID (Indonesia).

Thanks for the Contributors

Tejas N A for the PR. [sadegh] (https://github.com/sadeghpro) for the PR.

Credits

Michael Rozumyanskiy for libphonenumber-android

Harsh Bhakta for the original project at CountryCodePickerProject

egek92 for Turkish translation.

License

Apache Version 2.0

Copyright (C) 2019 Joielechong

Copyright (C) 2016 Harsh Bhakta

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.