Created
April 9, 2019 18:39
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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