Skip to content

Instantly share code, notes, and snippets.

@serhatleventyavas
Created November 10, 2020 09:02
Show Gist options
  • Save serhatleventyavas/9125ca1181f2ade17cecb7d11c21a154 to your computer and use it in GitHub Desktop.
Save serhatleventyavas/9125ca1181f2ade17cecb7d11c21a154 to your computer and use it in GitHub Desktop.
@SuppressLint("Recycle")
class CurrencyEditText @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = com.google.android.material.R.attr.editTextStyle
): TextInputEditText(context, attrs, defStyleAttr) {
private var current = ""
private val editText = this@CurrencyEditText
private var currency = ""
private var decimals = true
init {
addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
override fun onTextChanged(s: CharSequence, i: Int, i1: Int, i2: Int) {
val temp = s.toString().replace(",", "").replace(".", "")
if (temp != current) {
editText.removeTextChangedListener(this)
if (temp.isEmpty()) {
currency = "0,00"
editText.setText(currency)
editText.setSelection(editText.text.toString().length)
} else {
if (temp.length < 3) {
if (temp.length == 1) {
currency = "0,0$temp"
editText.setText(currency)
editText.setSelection(editText.text.toString().length)
} else if (temp.length == 2) {
currency = "0,$temp"
editText.setText(currency)
editText.setSelection(editText.text.toString().length)
}
} else {
var tempAmount = temp
while (tempAmount.isNotEmpty() && tempAmount.substring(0, 1) == "0") {
tempAmount = tempAmount.substring(1, tempAmount.length)
}
if (tempAmount.isEmpty()) {
currency = "0,00"
editText.setText(currency)
editText.setSelection(editText.text.toString().length)
} else {
tempAmount = tempAmount.replace(".", "").replace(",","")
if (tempAmount.length < 3) {
if (tempAmount.length == 1) {
currency = "0,0$tempAmount"
editText.setText(currency)
editText.setSelection(editText.text.toString().length)
} else if (tempAmount.length == 2) {
currency = "0,$tempAmount"
editText.setText(currency)
editText.setSelection(editText.text.toString().length)
}
} else {
val digitsCount = 2
val digitsAmount = tempAmount.takeLast(digitsCount)
val netAmount = tempAmount.substring(0, tempAmount.length - digitsCount)
currency = AmountConverter.convertAmount("$netAmount.$digitsAmount")
editText.setText(currency)
editText.setSelection(editText.text.toString().length)
}
}
}
}
editText.addTextChangedListener(this)
}
}
override fun afterTextChanged(editable: Editable) {}
})
}
val cleanDoubleValue: Double
get() {
if ((editText.text ?: "").isEmpty()) {
return 0.00
}
var value = 0.0
if (decimals) {
try {
val valueAsText = editText.text.toString().replace("[$,.]".toRegex(), "")
.replace(currency.toRegex(), "")
value = if (valueAsText.length < 3) {
"00.$valueAsText".toDouble()
} else {
val currencyAsText = valueAsText.substring(0, valueAsText.length - 2)
val currencyDigitsAsText = valueAsText.substring(valueAsText.length - 2)
"$currencyAsText.$currencyDigitsAsText".toDouble()
}
} catch (e: NumberFormatException) {
}
} else {
val cleanString = editText.text.toString().replace("[$,.]".toRegex(), "")
.replace(currency.toRegex(), "").replace("\\s+".toRegex(), "")
try {
value = cleanString.toDouble()
} catch (e: NumberFormatException) {
}
}
return value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment