here the code that with any symbol (m2, currency, kilos, etc)
fun EditText.addCurrencyFormatter(symbol: String) {
this.addTextChangedListener(object: TextWatcher {
private var current = ""
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (s.toString() != current) {
this@addCurrencyFormatter.removeTextChangedListener(this)
val cleanString = s.toString().replace("\\D".toRegex(), "")
val parsed = if (cleanString.isBlank()) 0.0 else cleanString.toInt()
val formatter = DecimalFormat.getInstance()
val formated = formatter.format(parsed).replace(",",".")
current = formated
this@addCurrencyFormatter.setText(formated + " $symbol")
this@addCurrencyFormatter.setSelection(formated.length)
this@addCurrencyFormatter.addTextChangedListener(this)
}
}
})
}
-use with-
edit_text.addCurrencyFormatter("TL")