Android edittext two decimal places

You should use InputFilter here is an example

public class DecimalDigitsInputFilter implements InputFilter {

Pattern mPattern;

public DecimalDigitsInputFilter(int digitsBeforeZero,int digitsAfterZero) {
    mPattern=Pattern.compile("[0-9]{0," + (digitsBeforeZero-1) + "}+((\\.[0-9]{0," + (digitsAfterZero-1) + "})?)||(\\.)?");
}

@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

        Matcher matcher=mPattern.matcher(dest);       
        if(!matcher.matches())
            return "";
        return null;
    }

}

you can use it like this

editText.setFilters(new InputFilter[] {new DecimalDigitsInputFilter(5,2)});

Kotlin solution using extension for EditText:

Create the following EditText decimal limiter function, which contains a TextWatcher which will look for text changes, such as checking the number of decimal digits & if the user enters only '.' symbol then it will prefix with 0.

fun EditText.addDecimalLimiter(maxLimit: Int = 2) {

    this.addTextChangedListener(object : TextWatcher {

        override fun afterTextChanged(s: Editable?) {
            val str = [email protected]!!.toString()
            if (str.isEmpty()) return
            val str2 = decimalLimiter(str, maxLimit)

            if (str2 != str) {
                [email protected](str2)
                val pos = [email protected]!!.length
                [email protected](pos)
            }
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

        }

    })
}

fun EditText.decimalLimiter(string: String, MAX_DECIMAL: Int): String {

    var str = string
    if (str[0] == '.') str = "0$str"
    val max = str.length

    var rFinal = ""
    var after = false
    var i = 0
    var up = 0
    var decimal = 0
    var t: Char

    val decimalCount = str.count{ ".".contains(it) }

    if (decimalCount > 1)
        return str.dropLast(1)

    while (i < max) {
        t = str[i]
        if (t != '.' && !after) {
            up++
        } else if (t == '.') {
            after = true
        } else {
            decimal++
            if (decimal > MAX_DECIMAL)
                return rFinal
        }
        rFinal += t
        i++
    }
    return rFinal
}

You may use it as follows:

val decimalText: EditText = findViewById(R.id.your_edit_text_id)
decimalText.addDecimalLimiter() // This will by default set the editText with 2 digit decimal
decimalText.addDecimalLimiter(3) // 3 or any number of decimals based on your requirements


Additional steps:

Also set inputType as numberDecimal in your layout file, which will show only number keypad

<EditText
android:inputType="numberDecimal" />

OR

You could set inputType programatically as follows:

decimalText.inputType = InputType.TYPE_CLASS_NUMBER

I took help from this post.