Skip to content

Instantly share code, notes, and snippets.

@Oleur
Created December 16, 2021 16:23
Show Gist options
  • Save Oleur/96c96540b1f0cb34ad50ceef46b0d1ad to your computer and use it in GitHub Desktop.
Save Oleur/96c96540b1f0cb34ad50ceef46b0d1ad to your computer and use it in GitHub Desktop.
Format Phone Number while typing with libphonenumber library
private val countryCode: String = Locale.getDefault().country
private val phoneNumberFormatter = PhoneNumberUtil.getInstance().getAsYouTypeFormatter(countryCode)
private fun getFormattedNumber(lastNonSeparator: Char, hasCursor: Boolean): String? {
return if (hasCursor) {
phoneNumberFormatter.inputDigitAndRememberPosition(lastNonSeparator)
} else {
phoneNumberFormatter.inputDigit(lastNonSeparator)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment