Skip to content

Instantly share code, notes, and snippets.

@eoinahern
Created April 9, 2019 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eoinahern/9d20a469579a25115b978cbc2114f28f to your computer and use it in GitHub Desktop.
Save eoinahern/9d20a469579a25115b978cbc2114f28f to your computer and use it in GitHub Desktop.
trying to format text in edittext using the Locale to add decimal places and comma seperators after user has typed
val df = DecimalFormat("#,###", DecimalFormatSymbols(Locale.getDefault()))
var textChanged = false
df.minimumFractionDigits = 0
df.maximumFractionDigits = asset.investmentDecimalPlaces
layout.txt_amount.textChanges()
.debounce(400, TimeUnit.MILLISECONDS)
.map { layout.txt_amount.text.toString() }
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
layout.txt_amount.setText(it)
layout.txt_amount.setSelection(it.length)
textChanged = true
}
.addTo(disposables)
layout.txt_amount.afterTextChangeEvents()
.debounce(2000, TimeUnit.MILLISECONDS)
.map { layout.txt_amount.text.toString() }
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
if (textChanged) {
val formatted = df.format(
it.replace(DecimalFormatSymbols(Locale.getDefault()).groupingSeparator.toString(), "")
.toDouble()
)
layout.txt_amount.setText(formatted)
layout.txt_amount.setSelection(formatted.length)
textChanged = false
}
}, {
it.printStackTrace()
})
.addTo(disposables)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment