Skip to content

Full Number Support

Harsh Bhakta edited this page Aug 26, 2017 · 13 revisions

Along with country and phone code selection, CCP offers all features which are required to support full number.

  1. Setup
  2. Auto-Formatting
  3. Number Validation
  4. Number Validity Change Listener
  5. Load Full Number
  6. Read Full Number

Full Number is concatenation of country code and carrier number i.e. if the country code is +1 and carrier number is 469 663 1766 then the full number will be +14696631766 or 14696631766.

1. SETUP

  1. Add this to your Gradle file and sync

      dependencies {
        compile 'com.hbb20:ccp:2.0.3'
      }

    *v2.0.3 might not the latest version. Check latest version from README file and use it instead of this.

  2. Add CCP view and editText of carrier number to XML layout

                      <LinearLayout
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:gravity="center_vertical">
                          
                          <com.hbb20.CountryCodePicker
                              android:id="@+id/ccp"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              app:ccp_countryPreference="us,in"
                              />
    
                          <EditText
                              android:id="@+id/editText_carrierNumber"
                              android:layout_width="match_parent"
                              android:layout_height="wrap_content"
                              android:editable="false"
                              android:hint="phone"
                              android:inputType="phone"
                              android:singleLine="true" />
                      </LinearLayout>
  3. Add CCP object in Activity / Fragment

      CountryCodePicker ccp;
      EditText editTextCarrierNumber;
    
  4. Bind CCP and Carrier Number editText from layout

      ccp = (CountryCodePicker) findViewById(R.id.ccp);
      editTextCarrierNumber = (EditText) findViewById(R.id.editText_carrierNumber);
    
  5. Attach CarrierNumber editText to CCP.

       ccp.registerCarrierNumberEditText(editTextCarrierNumber);
    

2. Auto Formatting

Auto formatting will keep carrier number formatted for selected country standerd. When you select country US (+1) and type 4696641777 in Carrier Edit Text, it will be formatted for US number format (469) 664-1777 as you type in.

By default, auto formatting is enabled for registered carrier number editText. So after successful setup , number in editTextCarrierNumber will be auto formatted as you type or load fullNumber in CCP.

To disable auto-formatting, add app:ccp_autoFormatNumber="false" property to CCP XML entry or call ccp.setNumberAutoFormattingEnabled(false);.

3. Number Validation

Once the setup is completed, you can validate entered full-number using ccp.isValidFullNumber();. Using this, a developer can keep end user from submitting wrong phone or invalid phone number.

4. Number Validity Change Listener

Validity Change Listener will get callBack every time validity of entered number changes.

ccp.setPhoneNumberValidityChangeListener(new CountryCodePicker.PhoneNumberValidityChangeListener() {
            @Override
            public void onValidityChanged(boolean isValidNumber) {
               // your code
            }
        });

5. Load Full Number

After setup, if you need to load existing full number, i.e. Edit Profile screen where you already have user's full phone number, then ccp.setFullNumber(existingFullNumber); should be used. This will auto-detect country & carrier number from full number and load in CCP & carrier number editText.

"+" in beginning of existingFullNumber is optional. "14696641766" or "+14696641766", both will set US +1 in CCP and (469) 664-1766 in carrier number edittext.

6. Read Full Number

After successful setup when the user has entered a number, you can read that full number in 3 different versions.

//get formatted number i.e "+1 (469) 664-1766"
ccp.getFormattedFullNumber();

//get unformatted number i.e. "14696641766"
ccp,.getFullNumber();

//get unformatted number with suffix "+" i.e "+14696641766"
ccp.getFullNumberWithPlus();
Clone this wiki locally