MaskedEditTextWatcher
Custom TextWatcher for EditText to mask phone number on-the-fly even with automatic country mask detection
Install / Use
/learn @MihaelIsaev/MaskedEditTextWatcherREADME
MaskedEditTextWatcher
Custom TextWatcher for EditText to mask phone number on-the-fly even with automatic country mask detection

How to install
via Gradle
compile 'com.mihaelisaev:masked-edit-text-watcher:1.0.0'
How to use
Manual list of masks
//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
//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.
Related Skills
node-connect
347.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
