You can refer below code using textwatcher
TextWatcher textWatcherNumber = new TextWatcher() {
boolean isEdging;
@Override
public void afterTextChanged(Editable text) {
if (isEdging) return;
isEdging = true;
StringBuilder sb = new StringBuilder();
sb.append(Common.parseOnlyNumbers(text.toString()));
if (sb.length() > 2)
sb.insert(2, ":");
text.replace(0, text.length(), sb.toString());
isEdging = false;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
};
//below is code of common class
public class Common {
public static String parseOnlyNumbers(String text) {
return text.replaceAll("[^0-9]","");
}
}
---- additional details ----
This way you can also limit user to add phone number in format (012)-345-6789-0000
you can replace below stringbuilder code in above function.
if (sb.length() > 0)
sb.insert(0, "(");
if (sb.length() > 4)
sb.insert(4, ")-");
if (sb.length() > 9)
sb.insert(9, "-");
if (sb.length() > 14)
sb.insert(14, "-");