Search code examples
androidkotlinandroid-edittextdecimalformatandroid-textwatcher

(Android) Unable to show dot as thousand separator in edit text using a pattern


Here, I have to show currency decimal separator and thousands separators as per the given input like:

    private var decimalSeparator: Char = '.'
    private var thousandSeparator: String = ","

    fun setDecimalSeparator(decimalSeparator: Char) {
        this.decimalSeparator = decimalSeparator
    }

    fun setThousandSeparator(thousandSeparator: String) {
        this.thousandSeparator = thousandSeparator
    }

And I have added text watcher for the edit text field to add a respective decimal and thousands separator like this with the help of Add comma as thousands separator for numbers in EditText for Android Studio?

field.addTextChangedListener(object : TextWatcher {

            
            override fun afterTextChanged(p0: Editable?) {
                Log.d("CharCount:", p0.toString().length.toString())
                field.removeTextChangedListener(this)

                try {
                    var givenstring: String = p0.toString()
                    if (givenstring.contains(thousandSeparator)) {
                        givenstring = givenstring.replace(thousandSeparator.toRegex(), "")
                    }
                    val longVal: Long = givenstring.toLong()
                    val formatter = DecimalFormat("#$thousandSeparator###$thousandSeparator###")
                    val formattedString = formatter.format(longVal)
                    field.setText(formattedString)
                    field.setSelection(field.text.length)
                    // to place the cursor at the end of text
                } catch (nfe: NumberFormatException) {
                    nfe.printStackTrace()
                } catch (e: Exception) {
                    e.printStackTrace()
                }

                field.addTextChangedListener(this)

            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                // no need any callback for this.
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                // no need any callback for this.
            }

        })

this is not working when thousandSeparator is period(.); can anyone help me with this?. Thanks in advance.


Solution

  • Here, this is how it worked out. I took help from https://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html and
    Add comma as thousands separator for numbers in EditText for Android Studio?

        field.addTextChangedListener(object : TextWatcher {
    
                // https://stackify.dev/354994-add-comma-as-thousands-separator-for-numbers-in-edittext-for-android-studio
                override fun afterTextChanged(p0: Editable?) {
                    field.removeTextChangedListener(this)
    
                    try {
                        var givenstring: String = p0.toString()
                        if (givenstring.contains(thousandSeparator)) {
                            givenstring = givenstring.replace(thousandSeparator.toString(), "")
                        }
                        val doubleVal: Double = givenstring.toDouble()
    
                        // https://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html
                        val unusualSymbols = DecimalFormatSymbols()
                        unusualSymbols.decimalSeparator = decimalSeparator
                        unusualSymbols.groupingSeparator = thousandSeparator
    
                        val formatter = DecimalFormat("#,##0.##", unusualSymbols)
                        formatter.groupingSize = 3
                        val formattedString = formatter.format(doubleVal)
    
                        field.setText(formattedString)
                        field.setSelection(field.text.length)
                        // to place the cursor at the end of text
                    } catch (nfe: NumberFormatException) {
                        nfe.printStackTrace()
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
    
                    field.addTextChangedListener(this)
    
                }
    
                override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                    // no need any callback for this.
                }
    
                override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                    // no need any callback for this.
                }
    
            })