Skip to content

Instantly share code, notes, and snippets.

@sajjadyousefnia
Created May 23, 2024 16:08
Show Gist options
  • Save sajjadyousefnia/33e183b1fb6282935a31fc20872b4903 to your computer and use it in GitHub Desktop.
Save sajjadyousefnia/33e183b1fb6282935a31fc20872b4903 to your computer and use it in GitHub Desktop.
import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import java.text.DecimalFormatSymbols
fun EditText.addThousandSeparator() {
this.addTextChangedListener(object : TextWatcher {
private var current = ""
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable?) {
if (s.toString() != current) {
val userInput = s.toString().replace("[^\\d]".toRegex(), "")
if (userInput.isNotEmpty()) {
val formatted = StringBuilder()
val symbols = DecimalFormatSymbols.getInstance()
val separator = symbols.groupingSeparator
var counter = 0
for (i in userInput.length - 1 downTo 0) {
formatted.append(userInput[i])
counter++
if (counter % 3 == 0 && i != 0) {
formatted.append(separator)
}
}
current = formatted.reverse().toString()
val selectionStart = this@addThousandSeparator.selectionStart
val selectionEnd = this@addThousandSeparator.selectionEnd
this@addThousandSeparator.removeTextChangedListener(this)
this@addThousandSeparator.setText(current)
this@addThousandSeparator.setSelection(calculateCursorPosition(selectionStart, selectionEnd, current))
this@addThousandSeparator.addTextChangedListener(this)
}
}
}
})
}
private fun calculateCursorPosition(start: Int, end: Int, text: String): Int {
val symbols = DecimalFormatSymbols.getInstance()
val separator = symbols.groupingSeparator
var newPosition = start
for (i in 0 until start) {
if (text[i] == separator) {
newPosition++
}
}
return newPosition.coerceAtMost(text.length)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment