7
votes

I have an editText where the user can enter an amount. So I want that this editText doesn't allow the user to enter more than two decimal places.

Example : 23.45 (not be 23.4567)

What's the best way to implement something like that?

6
Why you accepted my answer first and again accepted others answer.Abhishek kumar
Because at first, I didn't notice that your solution doesn't support negative numbersElias Dolinsek
Hi. Why you cannot use Edittext Picker Library as, it's mask function can easily solve your issue. Check this article: android.jlelse.eu/edittext-picker-library-4c71ae7d7863Ali Azaz Alam

6 Answers

17
votes

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)});
2
votes

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.

1
votes

you can go with below code :

or. Look into this : http://v4all123.blogspot.in/2013/05/set-limit-for-fraction-in-decimal.html

    et = (EditText) vw.findViewById(R.id.tx_edittext);

    et.setFilters(new InputFilter[] {
            new DigitsKeyListener(Boolean.FALSE, Boolean.TRUE) {
                int beforeDecimal = 5, afterDecimal = 2;

                @Override
                public CharSequence filter(CharSequence source, int start, int end,
                        Spanned dest, int dstart, int dend) {
                    String temp = et.getText() + source.toString();

                    if (temp.equals(".")) {
                        return "0.";
                    }
                    else if (temp.toString().indexOf(".") == -1) {
                        // no decimal point placed yet
                        if (temp.length() > beforeDecimal) {
                            return "";
                        }
                    } else {
                        temp = temp.substring(temp.indexOf(".") + 1);
                        if (temp.length() > afterDecimal) {
                            return "";
                        }
                    }

                    return super.filter(source, start, end, dest, dstart, dend);
                }
            }
    });

or,

et.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    String text = arg0.toString();
    if (text.contains(".") && text.substring(text.indexOf(".") + 1).length() > 2) {
        et.setText(text.substring(0, text.length() - 1));
        et.setSelection(et.getText().length());
    }
}

public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

}

public void afterTextChanged(Editable arg0) {
}
});
0
votes
           input.addTextChangedListener(new TextWatcher() {

                public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                    String text = arg0.toString();
                    if (arg0.length() <= 1) {
                        if (text.contains(".") && text.indexOf(".") == 0) {
                            holder.input.setText("0.");
                            holder.input.setSelection(holder.input.getText().length());
                        }

                    } else {
                        if (text.contains(".") &&
                                text.indexOf(".") != text.length() - 1 &&
                                String.valueOf(text.charAt(text.length() - 1)).equals(".")) {
                            holder.input.setText(text.substring(0, text.length() - 1));
                            holder.input.setSelection(holder.input.getText().length());
                        }
                        if (text.contains(".") && text.substring(text.indexOf(".") + 1).length() > 2) {
                            holder.input.setText(text.substring(0, text.length() - 1));
                            holder.input.setSelection(holder.input.getText().length());
                        }
                    }

                }

                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

                }

                public void afterTextChanged(Editable arg0) {
                }
            });
0
votes

Kotlin:

Keep below extension function inside of any kotlin file in your project not in class. (can be outside of class block or a separate file to access it globally)

fun String.removeAfter2Decimal(et: EditText) {
return if (this.isNullOrEmpty() || this.isNullOrBlank() || this.toLowerCase() == "null") {
    //
} else {
    if(this.contains(".")) {
        var lastPartOfText = this.split(".")[this.split(".").size-1]

        if (lastPartOfText.count() > 2) {
            try {
                lastPartOfText = this.substring(0, this.indexOf(".")+3)
                et.setText(lastPartOfText)
                et.setSelection(lastPartOfText.length)
            } catch (e: Exception) {
                e.printStackTrace()
            }

        } else {

        }
    } else {

    }
  }
}

Now Use like below:

myEditText.addTextChangedListener(object : TextWatcher {

        override fun afterTextChanged(editable: Editable?) {

        }

        override fun beforeTextChanged(cs: CharSequence?, p1: Int, p2: Int, p3: Int) {

        }

        override fun onTextChanged(cs: CharSequence?, p1: Int, p2: Int, p3: Int) {
            
            val mText = binding.etAmount.text.toString()

            mText.removeAfter2Decimal(binding.etAmount) // This will help to remove after 2 decimal text

        }
    })

P.S: I need TextWatcher for other works so I didn't place TextWatcher inside the custom function. This Works nicely in my project.

Thank :)

0
votes

Kotlin solution with InputFilter

class DecimalDigitsInputFilter(digitsBeforeZero: Int, digitsAfterZero: Int) : InputFilter {

    //                                             digitsBeforeZero  or       digitsBeforeZero + dot + digitsAfterZero
    private val pattern = Pattern.compile("(\\d{0,$digitsBeforeZero})|(\\d{0,$digitsBeforeZero}\\.\\d{0,$digitsAfterZero})")

    override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dstart: Int, dend: Int): CharSequence? {
        return if (source.isEmpty()) {
            // When the source text is empty, we need to remove characters and check the result
            if (pattern.matcher(dest.removeRange(dstart, dend)).matches()) {
                // No changes to source
                null
            } else {
                // Don't delete characters, return the old subsequence
                dest.subSequence(dstart, dend)
            }
        } else {
            // Check the result
            if (pattern.matcher(dest.replaceRange(dstart, dend, source)).matches()) {
                // No changes to source
                null
            } else {
                // Return nothing
                ""
            }
        }
    }
}

You can use it like this:

editText.filters = arrayOf(DecimalDigitsInputFilter(5, 2))