Creating a class that can format string input inside EditText into a phone number. However, the input return isn’t appending the “-“.
internal class PhoneNumberFormatter : TextFormatter {
override fun format(input: String): String {
// logic to format phone #
val textLength = input.length;
if (input.endsWith("-") || input.endsWith(" ") || input.endsWith(" "))
return ""
if (textLength == 4) {
if (!input.contains("-")) {
StringBuilder(input).append("-").toString()
}
} else if (textLength == 8) {
if (input.contains("-")) {
StringBuilder(input).insert(input.length - 1, "-").toString()
}
}
return input
}
}
1