Skip to content

Instantly share code, notes, and snippets.

@hitesh-dhamshaniya
Created April 17, 2020 09:05
Show Gist options
  • Save hitesh-dhamshaniya/d7d32f5205f375170bcd9d6108ccbb15 to your computer and use it in GitHub Desktop.
Save hitesh-dhamshaniya/d7d32f5205f375170bcd9d6108ccbb15 to your computer and use it in GitHub Desktop.
Phone Number Watcher is utility class to format mobile number in android 000-000-0000, In most of application we required phone number formater, so that I created gist, so easily get whenever required. Hope it will help.
import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
/**
* @author Hitesh
* @version 1.0
* @since 14-08-2019
*/
class PhoneNumberWatcher : TextWatcher {
private val mEditText: EditText
private var beforeText: String? = null
private var type: Int = 0
constructor(editText: EditText) {
this.mEditText = editText
}
fun setType(type: Int) {
this.type = type
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
beforeText = mEditText.text.toString().trim { it <= ' ' }
}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable) {
mEditText.removeTextChangedListener(this)
var text = mEditText.text.toString()
var strictNTxt = strictTrim(text)
val strictOTxt = strictTrim(beforeText!!)
if (strictNTxt.length > 10) {
strictNTxt = strictOTxt
text = strictNTxt
}
if (text.length > beforeText!!.length) {
text = strictNTxt
} else {
if (strictOTxt.length > strictNTxt.length) {
text = strictNTxt
} else {
val index = match(text, beforeText!!)
if (index == 0) {
//Do Nothing
} else if (index == -1) {
if (text.length > 0 && !Character.isLetterOrDigit(beforeText!![beforeText!!.length - 1])) {
text = text.substring(0, text.length - 1)
}
} else {
if (!Character.isLetterOrDigit(beforeText!![index])) {
text = text.substring(0, index - 1) + text.substring(index - 1, text.length)
} else {
//Do Nothing
}
}
}
}
val formattedValue = formatPhoneNumber(text)
mEditText.setText(formattedValue)
mEditText.setSelection(mEditText.text.length)
mEditText.addTextChangedListener(this)
}
private fun strictTrim(text: String): String {
val sb = StringBuilder()
for (c in text.toCharArray()) {
if (Character.isLetterOrDigit(c)) {
sb.append(c)
}
}
return sb.toString()
}
fun formatPhoneNumber(text: String): String {
var text = text
text = trim(text)
if (text.length > 10) {
text = text.substring(0, 3) + "-" + text.substring(3, 6) + "-" + text.substring(
6,
10
) + "(" + text.substring(10) + ")"
} else if (text.length > 6) {
text = text.substring(0, 3) + "-" + text.substring(3, 6) + "-" + text.substring(6)
} else if (text.length > 3) {
text = text.substring(0, 3) + "-" + text.substring(3)
}
return text
}
fun trim(text: String): String {
var text = text
text = text.trim { it <= ' ' }
text = text.replace("-", "")
text = text.replace("(", "")
text = text.replace(")", "")
return text
}
private fun match(text1: String, text2: String): Int {
val textArray1 = text1.toLowerCase().toCharArray()
val textArray2 = text2.toLowerCase().toCharArray()
val minLength = Math.min(textArray1.size, textArray2.size)
for (i in 0 until minLength) {
if (textArray1[i] != textArray2[i]) {
return i
}
}
return -1
}
/*
* This will remove all symbol
*/
fun getStrippedPhoneNumber(phoneNumber: String): String {
// Remove all non-digit characters
return phoneNumber.replace("\\D+".toRegex(), "")
}
}
/**
* How to use with edit text
*/
val edtPhoneNumber = mViewDataBinding.edtContactPhone.getEditText()
val mPhoneNumberWatcher = PhoneNumberWatcher(edtPhoneNumber)
edtPhoneNumber.addTextChangedListener(mPhoneNumberWatcher)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment