3

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++;
              }
          }
      }
  });
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Steve Kamau
  • 2,755
  • 10
  • 42
  • 73

1 Answers1

4

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.

AlbAtNf
  • 3,859
  • 3
  • 25
  • 29
  • Another problem, how do i limit to only one decimal point input? – Steve Kamau Mar 07 '16 at 08:25
  • 2
    Nevermind i did it using `mEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);` – Steve Kamau Mar 07 '16 at 09:31
  • 1
    https://developer.android.com/reference/java/text/DecimalFormat.html and other such classes give you the option of dynamically getting the region and then using the "decimal point" and "thousands separator" as is appropriate "." and "," or "," and "." based on region. That way you don't have to hard code characters like you are here (more adaptable) – mawalker May 30 '16 at 04:31