1

What is my requirement is when the user enter 10 digits,a Dot and two decimal points(0123456789.00).Once the user entered this format automatically the edit text should stop adding text into it.And the user should not enter more than one dot.

is it possible..?.need help

thanks in advance..!

Asif Sb
  • 755
  • 9
  • 35

4 Answers4

2

You can apply text filter to your EditText by using editText.setFilters(filter), So the method used will be like the following:

text = (EditText) findViewById(R.id.text);
validate_text(text);

// the method validate_text that forces the user to a specific pattern
protected void validate_text(EditText text) {

    InputFilter[] filter = new InputFilter[1];
    filter[0] = new InputFilter() {

        @Override
        public CharSequence filter(CharSequence source, int start, int end,
                Spanned dest, int dstart, int dend) {

            if (end > start) {
                String destText = dest.toString();
                String resultingText = destText.substring(0, dstart)
                        + source.subSequence(start, end)
                        + destText.substring(dend);
                if (!resultingText
                        .matches("^\\d{1,10}(\\.(\\d{1,2})?)?")) {
                    return "";
                } 
            }

            return null;
        }
    };
    text.setFilters(filter);
}

This will force the user to enter "dot" after entering for digits, and will force him to enter only "two digits" after the "dot".

Muhammed Refaat
  • 8,096
  • 11
  • 76
  • 108
0

You can add a TextWatcher to your edit text and listen for the text editing events.

Manish
  • 1,085
  • 8
  • 27
0

You will be interested in TextWatcher. You would eventually do EditText.addTextChangedListener (TextWatcher) .

Vino
  • 1,536
  • 1
  • 18
  • 25
  • yes...can u tell me how to stop the edit text to enter a new text when this format completes here in text watcher? – Asif Sb May 13 '15 at 10:22
  • Perhaps this post may give you some direction - http://stackoverflow.com/questions/18305520/edittext-custom-string-formatting – Vino May 13 '15 at 10:30
0

I didn't try that, but it should work.

final Editable text = new SpannableStringBuilder("example");
boolean stop = false;

et.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        if(stop) {
            s = text;
        } else if (text.toString().equals(s.toString())) {
            stop = true;
        }
    }
});
Muzaffer
  • 1,422
  • 1
  • 13
  • 22