1

What I'm trying to do is limit the range of values that can be inserted in a EditText, like a NumberPicker. Why won't I use NumberPicker? Because it needs API 11 and I'm want my application to be compatible with API 10 and above. I have set the range from 1 to 120. If a user enters a number outside that range, the text in the EditText will change to 15.

I have this code that works however I think it's not the best way to implement this.

final EditText ed = ((EditText) findViewById(R.id.editMinutes));
TextWatcher tw = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

    @Override
    public void afterTextChanged(Editable s) {
        }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        try {
            int minutes = Integer.valueOf(s.toString());
            if (minutes < 1 || minutes > 120) {
                ed.setText("15");
            }
        } catch (NumberFormatException ex) {
            ed.setText("15");
        }
    }
};
ed.addTextChangedListener(tw);

How can I improve this? Is there a better or more elegant way of doing this?

The Berga
  • 3,744
  • 2
  • 31
  • 34
  • 2
    IF you really want a number picker- go into the AOSP source code, find number picker, and add it to your app then use it. There's no hardware support needed for number picker, its just a GUI component. https://gitorious.org/atrix-aosp/frameworks_base/source/d762f063be970033314d3f77194bfe5cb284b605:core/java/android/widget/NumberPicker.java – Gabe Sechan Dec 22 '14 at 02:18
  • I second that, I was looking at the code it is simple drop in. – user210504 Dec 22 '14 at 02:20
  • I don't think I'm allowed to use external code. At least not of that size. – The Berga Dec 31 '14 at 00:10

1 Answers1

0

you can use the input filter to define a regular expression

    InputFilter filter= new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

                String checkMe = dest.toString()+ source.toString();
                Pattern pattern = Pattern.compile("([1-9][0-9]?|1[01][0-9]|120)");
                Matcher matcher = pattern.matcher(checkMe);
                boolean valid = matcher.matches();
                if(!valid){
                    Log.i("", "invalid");
                    return "";
                }else{
                    Log.i("", "valid less than 120");
                }
            return null;

        }
    };

    EditText editText=(EditText)findViewById(R.id.editMinutes);
    editText.setFilters(new InputFilter[]{filter});

more elegant code for text watcher taken from here

 public abstract class TextValidator implements TextWatcher {
private final TextView textView;

public TextValidator(TextView textView) {
    this.textView = textView;
}

public abstract void validate(TextView textView, String text);

@Override
final public void afterTextChanged(Editable s) {
    String text = textView.getText().toString();
    validate(textView, text);
}

@Override
final public void beforeTextChanged(CharSequence s, int start, int count, int after) { /* Don't care */ }

@Override
final public void onTextChanged(CharSequence s, int start, int before, int count) { /* Don't care */ }
 }

use it like this:

 editText.addTextChangedListener(new TextValidator(editText) {
  @Override public void validate(TextView textView, String text) {
   /* Validation code here */
   }
});
Community
  • 1
  • 1
M.Khouli
  • 3,992
  • 1
  • 23
  • 26