12

I've an edittext , it only gets numeric without decimal numbers.

android:inputType="number"

I want to separate thousands while I'm typing . For example 25,000 .

I know I should use TextWatcher and I've used this code but I couldn't make it work :

@Override
        public void afterTextChanged(Editable viewss) {
            String s = null;
            try {
                // The comma in the format specifier does the trick
                s = String.format("%,d", Long.parseLong(viewss.toString()));
            } catch (NumberFormatException e) {
            }

        }

Could you help me to do so ?

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
mahdi yamani
  • 923
  • 3
  • 12
  • 29

3 Answers3

1

add this into your app's Gradle compile 'com.aldoapps:autoformatedittext:0.9.3' in XML xmlns:app="http://schemas.android.com/apk/res-auto"

<com.aldoapps.autoformatedittext.AutoFormatEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:isDecimal="true"
    android:maxLength="8"
    android:id="@+id/number"/>

This lib automatically add (,) (.)

TechVINO
  • 53
  • 1
  • 1
  • 7
1

Kotlin version of Adrian Cid Almageur's comment

import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import java.text.DecimalFormat
import java.text.ParseException

class NumberTextWatcher(private val editText: EditText) : TextWatcher {

    private val decimalFormat = DecimalFormat("#,###.##")
    private val decimalFormatNoFrac = DecimalFormat("#,###")

    private var hasFractionalPart = false

    init {
        decimalFormat.isDecimalSeparatorAlwaysShown = true
    }

    override fun afterTextChanged(s: Editable?) {
        editText.removeTextChangedListener(this)

        try {
            val initialLength = editText.text.length

            val v = s.toString()
                .replace(decimalFormat.decimalFormatSymbols.groupingSeparator.toString(), "")
            val number = decimalFormat.parse(v)

            val cp = editText.selectionStart
            if (hasFractionalPart) {
                editText.setText(decimalFormat.format(number))
            } else {
                editText.setText(decimalFormatNoFrac.format(number))
            }
            val endLength = editText.length()
            val selection = (cp + (endLength - initialLength))
            if (selection > 0 && selection <= editText.text.length) {
                editText.setSelection(selection)
            } else {
                editText.setSelection(editText.text.length - 1)
            }
        } catch (nfe: NumberFormatException) {

        } catch (ex: ParseException) {

        }

        editText.addTextChangedListener(this)
    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        hasFractionalPart =
            s.toString().contains(decimalFormat.decimalFormatSymbols.decimalSeparator)
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
}
0
DecimalFormat formatter = new DecimalFormat("#,###,###");
String yourFormattedString = formatter.format(100000);

Use DecimalFormat