0

Can someone please help me? How I can force user to enter the numberDecimal in EditText field in XX.XX format(Eg.56.75).

I used max length attribute to restrict max length to 5.

Tung Tran
  • 2,885
  • 2
  • 17
  • 24
Siva Prasad
  • 115
  • 1
  • 13

6 Answers6

2

You can go the easiest way

<EditText
   //.....
    android:inputType="numberDecimal" /> // set inputformat

You can use InputFilter if you need to make sure there are 2 digits before . and after.

Try this under onCreate

youreditText.setFilters(new InputFilter[] {new DecimalDigitsInputFilter()});

this is just a class for this

EDIT: Had to rewrite the pattern as it seems that matcher matches the input starting with the first character and checks each one so you need to add a lot of optional parts in the patterns so that it passes each check that is being made. Now it doesn't allow 222 as it did in your case

public class DecimalDigitsInputFilter implements InputFilter {

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        if (end > start) {
            String destTxt = dest.toString();
            String resultingTxt = destTxt.substring(0, dstart) + source.subSequence(start, end) + destTxt.substring(dend);
            if (!resultingTxt.matches("^\\d(\\d(\\.\\d{0,2})?)?")) {
                return "";
            }
        }
        return null;
    }
}
Rainmaker
  • 10,294
  • 9
  • 54
  • 89
  • I tried this code but I could only enter either XX or .XX (Eg : either 67 or .67) It is only allowing two digits or decimal and two digits after it – Siva Prasad Mar 21 '18 at 08:16
  • I edited the regex and checked it , now it matches 21.21 for example – Rainmaker Mar 21 '18 at 08:24
  • `mPattern=Pattern.compile("[0-9]{" + digitsBeforeZero + "}\\.[0-9]{" + digitsAfterZero + "}");` I tried this, this time it is not letting anything to enter – Siva Prasad Mar 21 '18 at 08:30
  • "[0-9]{digitsBeforeZero}\\.[0-9]{digitsAfterZero}" I edited it this way it matches, check – Rainmaker Mar 21 '18 at 08:32
  • When I am using that `digitsBeforeZero` directly without string concatenation, IDE showing error "number expected here" – Siva Prasad Mar 21 '18 at 08:33
  • that's weird, just hard code 2 to try then "[0-9]{2}\\.[0-9]{2}", as you always expect 2 and 2 and then you can look into why it doesn't allow it, as we declared these variables as ints – Rainmaker Mar 21 '18 at 08:36
  • In this way, this is not letting anything to enter. It is not taking at all – Siva Prasad Mar 21 '18 at 08:58
  • `[0-9]{0,2}+((\.[0-9]{0,1})?)||(\.)?` This is working and taking values in `xx.xx`, `x.xx`, `.xx` formats. But it is also taking values in `xxx` format too. Once I type 3 numbers in a row, after that 3 numbers it is not letting me to enter dot or any other number. And user should not able to enter `xxx` format. – Siva Prasad Mar 22 '18 at 05:41
  • @SivaPrasad wow,man! It wasn't easy but after an hour in the studio it finally does the trick. Sorry, for my late response! I edited my ancwer, please check. Had to create a sample app for this case – Rainmaker Mar 22 '18 at 09:36
1

you can try

        TextWatcher t1;
        EditText e;
        t1=new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if(s.length()==3 && !s.toString().contains("."))
                {
                    e.removeTextChangedListener(t1);
                    e.setText(s.subSequence(0,1)+"."+s.charAt(2));
                    e.addTextChangedListener(t1);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {

                // TODO Auto-generated method stub
            }
        };
        e.addTextChangedListener(t1);
Jyoti JK
  • 2,141
  • 1
  • 17
  • 40
0

try this:

edittext.addTextChangedListener(new TextWatcher() {

   @Override
   public void afterTextChanged(Editable s) {}

   @Override    
   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   @Override    
   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      DecimalFormat formatter = new DecimalFormat("##.##");
      String yourFormattedString = formatter.format(s.toString());
      edittext.setText(yourFormattedString);
   }
  });
Cao Minh Vu
  • 1,900
  • 1
  • 16
  • 21
0

Try this

exitText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if(charSequence.length() == 2){
                exitText.setText("");
                exitText.setText(charSequence+".");
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });
SAKhan
  • 249
  • 4
  • 16
0

If you want to have decimal number format, you can use this in XML:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal" />

To limit the number of digits, you might need to use InputFilter mentioned in other answers or here: Limit Decimal Places in Android EditText

Micer
  • 8,731
  • 3
  • 79
  • 73
0

This following code is also working :

Inside OnCreateView :

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

Java Class :

public class DecimalDigitsInputFilter implements InputFilter {


    final int maxDigitsBeforeDecimalPoint ;
    final int maxDigitsAfterDecimalPoint ;

    public DecimalDigitsInputFilter(int digitsBeforeDecimal,int digitsAfterDecimal ){
        maxDigitsBeforeDecimalPoint = digitsBeforeDecimal;
        maxDigitsAfterDecimalPoint = digitsAfterDecimal;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end,
                               Spanned dest, int dstart, int dend) {
        StringBuilder builder = new StringBuilder(dest);
        builder.replace(dstart, dend, source
                .subSequence(start, end).toString());
        if (!builder.toString().matches(
                "(([1-9]{1})([0-9]{0," + (maxDigitsBeforeDecimalPoint - 1) + "})?)?(\\.[0-9]{0," + maxDigitsAfterDecimalPoint + "})?"

        )) {
            if (source.length() == 0)
                return dest.subSequence(dstart, dend);
            return "";
        }

        return null;

    }


}
Siva Prasad
  • 115
  • 1
  • 13