Search code examples
androidandroid-edittextmasking

How to mask EditText to take input in specific format


I'm trying to restrict what my EditText takes as input. I want it to take a maximum of 21 million (20,999,999.99999999). Is it possible to achieve> if, then how do I go about it? I've tried using InputFilter with a pattern [0-9]{0,9}+((\.[0-9]{0,7})?)||(\.)?. Show me how to do it.


Solution

  • Well, I read Francesco's answer and your comment also. If you need to check the decimal point, you can try this below.

    EditText someText = findViewById(R.id.someValueInputHi);
    String paw = someText.getText().toString().replaceAll("\\s+", ""); //rem unusual spaces
    Double someInput = Double.parseDouble(paw);
    if (someInput < 5.3465726543d | someInput > 3.7834657836d) {
        //Ok now
    } else {
        //Oops, no man
    }
    

    You only take the value between 5.3465726543 and 3.7834657836.

    Why double?

    As the name implies, a double has 2x the precision of float. In general, a double has 15 decimal digits of precision, while float has 7.

    EDIT: Francesco's answer is now deleted.