Search code examples
javaandroidandroid-edittextandroid-textwatcher

How Edittext can accept only two numbers after decimal point


I am trying to add only two numbers after a decimal point input in an EditText.

So I implemented a TextWatcher to check the string during input.

The function I am using below works amazing but has one major flaw. When you input any value,then you add a decimal point,delete that decimal point and proceed to add more values,only 3 values are accepted as input.

Case example: I input 300. but then I realize I wanted to input 3001234567, so I delete the decimal point . and proceed to add 1234567 to 300, Only 123 will be accepted and the rest ignored.

How should i handle this? Any suggestions will be appreciated.

My code:

 price.addTextChangedListener(new TextWatcher() {
     public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

     }

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

     }

     public void afterTextChanged(Editable arg0) {
         if (arg0.length() > 0) {
             String str = price.getText().toString();
             price.setOnKeyListener(new View.OnKeyListener() {
                 public boolean onKey(View v, int keyCode, KeyEvent event) {
                     if (keyCode == KeyEvent.KEYCODE_DEL) {
                         count--;
                         InputFilter[] fArray = new InputFilter[1];
                         fArray[0] = new InputFilter.LengthFilter(100);
                         price.setFilters(fArray);
                         //change the edittext's maximum length to 100.
                         //If we didn't change this the edittext's maximum length will
                         //be number of digits we previously entered.
                     }
                     return false;
                 }
             });
             char t = str.charAt(arg0.length() - 1);
             if (t == '.') {
                 count = 0;
             }
             if (count >= 0) {
                 if (count == 2) {
                     InputFilter[] fArray = new InputFilter[1];
                     fArray[0] = new InputFilter.LengthFilter(arg0.length());
                     price.setFilters(fArray);
                     //prevent the edittext from accessing digits
                     //by setting maximum length as total number of digits                               we typed till now.
                 }
                  count++;
              }
          }
      }
  });

Solution

  • Try this:

    @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                 String input = s.toString();
                if(input.contains(".") && s.charAt(s.length()-1) != '.'){
                    if(input.indexOf(".") + 3 <= input.length()-1){
                        String formatted = input.substring(0, input.indexOf(".") + 3);
                        editReceiver.setText(formatted);
                        editReceiver.setSelection(formatted.length());
                    }
                }else if(input.contains(",") && s.charAt(s.length()-1) != ','){
                    if(input.indexOf(",") + 3 <= input.length()-1){
                        String formatted = input.substring(0, input.indexOf(",") + 3);
                        editReceiver.setText(formatted);
                        editReceiver.setSelection(formatted.length());
                    }
                }
            }
    

    Please note, german decimals are , seperated instead of . seperated You can remove that else part, if it is not needed.