0

I've stucked with the following problem: I need to validate, that EditText's value is an Integer between 1 and 1000. If it's not - I should put some of the default value into the EditText. When I try to do such a thing with following code - I get to the infinite loop (well, that was predictable). I've checked the similar questions, but I still can't figure out, how to make the code working.
Are there any other ways to implement the desirable behaviour? How I should edit my code to achieve this?

    Long mailIntervalValue;
    EditText etMailInterval;
        .
        .
        .
        .
    etMailInterval=(EditText)findViewById(R.id.et_mail_check_interval);
    etMailInterval.setText(mailIntervalValue.toString());
    etMailInterval.addTextChangedListener(new TextWatcher(){
        public void afterTextChanged(Editable s) {
            Integer t=Integer.getInteger(s.toString());
            if (t==null){
                s.clear();
                s.append(mailIntervalValue.toString());
                mailIntervalValue=MessageManager.DEFAULT_TIME;
            }
            else
                mailIntervalValue=t.longValue();
            if (mailIntervalValue<1 || mailIntervalValue>1000){
                if (mailIntervalValue<1)
                    mailIntervalValue=1L;
                else
                    mailIntervalValue=1000L;
                s.clear();
                s.append(mailIntervalValue.toString());
                Toast.makeText(MainActivity.this, MainActivity.this.getString(R.string.settings_timer_fail),
                        Toast.LENGTH_SHORT).show();
            }

            saveMailerPrefs();
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count){}
    });

3 Answers 3

1

I suggest using the intended tool for the job: an input filter might be a better alternative.

Here is some example code and some documentation. If you need something more complex, I recommend looking at this code.

Sign up to request clarification or add additional context in comments.

Comments

0

Did you notice that TextWatcher is calling itself? I think it would be a better approach to check validation with any other event e.g. On Enter press or with a button etc..

1 Comment

Of course, I've noticed it ) And the question is - how I can avoid it and still get the results. OnEnter, or any other button press is not appropriate for this case, cause user can click on the button - which takes him out of this layout - so he can't see that the program corrected him. (Well, he actually can see it by the Toast, but still there is an other layout in front of him right now, which is not so user-friendly)
0

You can use z-validations library for your case, there is InRange validation - it's exactly what you need. Sample code using library:

//create validation form
Form form = new Form(mActivity);
form.addField(Field.using(mYourEditText).validate(InRange.build(mContext, minAllowed, maxAllowed)));

//validate form

if (form.isValid()) {
    Toast.makeText(this, "Form is valid", Toast.LENGTH_SHORT).show();
}

More about EditText validation and this library you can read in post: Android form validation - the right way

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.