<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mihaelisaev</groupId>
  <artifactId>masked-edit-text-watcher</artifactId>
  <version>1.0.0</version>
  <packaging>aar</packaging>
  <name>mask-edit-text-watcher</name>
  <description># MaskedEditTextWatcher
Custom TextWatcher for EditText to mask phone number on-the-fly even with automatic country mask detection

![Demonstration GIF](https://github.com/MihaelIsaev/MaskedEditTextWatcher/raw/master/stuff/example.gif)

## How to use

### Manual list of masks
```java
//Instantiate your EditText
EditText phoneTextField = (EditText) findViewById(R.id.phoneTextField);
//Create text watcher
MaskedEditTextWatcher simpleListener = new MaskedEditTextWatcher(phoneTextField, new MaskedEditTextWatcherDelegate() {
    @Override
    public String maskForCountryCode(String text) {
        //Here you receive just entered text
        //and you should return the mask or null
        if (text.equals("1")) {
            return "+1 ###-###-####";
        } else if (text.equals("7")) {
            return "+7 (###) ###-##-##";
        } else if (text.equals("44")) {
            return "+44 (##) ###-####";
        } else if (text.equals("64")) {
            return "+64 ## # (###) ##-##";
        }
        return null;
    }
});
//Add the textWatcher to your text field
phoneTextField.addTextChangedListener(simpleListener);
```

### Get mask from google phone library

First of all add the following line to your app module gradle file to install the google phone library
```
compile 'com.googlecode.libphonenumber:libphonenumber:7.1.1'
```

Then just implement MaskedEditTextWatcherDelegate using google phone library
```java
//Instantiate your EditText
EditText phoneTextField = (EditText) findViewById(R.id.phoneTextField);
//Create text watcher
MaskedEditTextWatcher glibListener = new MaskedEditTextWatcher(phoneTextField, new MaskedEditTextWatcherDelegate() {
    @Override
    public String maskForCountryCode(String text) {
        //Here you receive just entered text
        //and you should return the mask or null
        PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
        try {
            //We just should check if entered text is numeric
            Double.parseDouble(text);
            //Then we should get the region code based on first entered digits
            String regionCode = phoneUtil.getRegionCodeForCountryCode(Integer.parseInt(text));
            //Then we get an example number for this region
            Phonenumber.PhoneNumber exampleNumber = phoneUtil.getExampleNumber(regionCode);
            //We should check the number and if it's null then we alse return null
            if (exampleNumber == null) return null;
            //Here we get country code as digits
            int countryCodeForRegion = phoneUtil.getCountryCodeForRegion(regionCode);
            //Here we create the string with country code with + symbol
            String detectedCountryCode = "+" + countryCodeForRegion;
            //Here we create example number but wothout country code
            String example = phoneUtil.format(exampleNumber, PhoneNumberUtil.PhoneNumberFormat.NATIONAL);
            example = example.replace("8 ", ""); //needed for Russian example number
            example = example.replace(detectedCountryCode, "");
            //And finally we create full mask with country code
            return detectedCountryCode+" "+example.replaceAll("\\d", "#");
        } catch (NumberFormatException e) {
            //If entered text is not numeric then we shoul return null
            return null;
        }
    }
});
//Add the textWatcher to your text field
phoneTextField.addTextChangedListener(glibListener);
```

*This lib is under Apache 2.0 license.*
</description>
  <url>https://github.com/MihaelIsaev/MaskedEditTextWatcher</url>
  <inceptionYear>2017</inceptionYear>
  <licenses>
    <license>
      <name>Apache 2.0</name>
      <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
      <distribution>repo</distribution>
    </license>
  </licenses>
  <developers>
    <developer>
      <id>mihael</id>
      <name>Mikhail Isaev</name>
      <email>me@mihaelisaev.com</email>
    </developer>
  </developers>
  <scm>
    <connection>https://github.com/MihaelIsaev/MaskedEditTextWatcher.git</connection>
    <developerConnection>https://github.com/MihaelIsaev/MaskedEditTextWatcher.git</developerConnection>
    <url>https://github.com/MihaelIsaev/MaskedEditTextWatcher</url>
  </scm>
  <dependencies>
    <dependency>
      <groupId>com.android.support</groupId>
      <artifactId>appcompat-v7</artifactId>
      <version>25.2.0</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>
