0

I have an edittext and I want to restrict the numerical values inserted in that edittext between 18 and 85. I am currently using this InputFilter over my edittext. But not useful for me

public class InputFilterMinMax implements InputFilter {

private int min, max;

public InputFilterMinMax(int min, int max) {
    this.min = min;
    this.max = max;
}

public InputFilterMinMax(String min, String max) {
    this.min = Integer.parseInt(min);
    this.max = Integer.parseInt(max);
}

@Override
public CharSequence filter(CharSequence source, int start, int end,
        Spanned dest, int dstart, int dend) {
    try {
        // Remove the string out of destination that is to be replaced
        String replacement = source.subSequence(start, end).toString();
        String newVal = dest.subSequence(0, dstart).toString()
                + replacement
                + dest.subSequence(dend, dest.length()).toString();
        int input = Integer.parseInt(newVal);
        if (isInRange(min, max, input))
            return null;
    } catch (NumberFormatException nfe) {
    }
    return "";
}

private boolean isInRange(int a, int b, int c) {
    return b > a ? c >= a && c <= b : c >= b && c <= a;
}
}

Please help me out.. Thanks

user2781627
  • 455
  • 5
  • 17
  • Please learn to search in Google first : http://stackoverflow.com/questions/5391394/edittext-values-in-range – Dhaval Jul 09 '14 at 10:37
  • http://tech.chitgoks.com/2011/06/27/android-set-min-max-value-an-edittext-accepts/ – Dhaval Jul 09 '14 at 10:38
  • http://stackoverflow.com/questions/14212518/is-there-any-way-to-define-a-min-and-max-value-for-edittext-in-android – karan Jul 09 '14 at 11:17

3 Answers3

0

You can assign a TextWatcher to your EditText and then listen for text changes there, For example:

public void afterTextChanged(Editable s) {
   try {
     int val = Integer.parseInt(s.toString());
     if(val > 85) {
        replace(0, length(), "85", 0, 2);
     } else if(val < 18) {
        replace(0, length(), "18", 0, 2);
     }
   } catch (NumberFormatException ex) {
      // Do something
   }
}
CodeWarrior
  • 5,026
  • 6
  • 30
  • 46
0

You can use textwatcher on this editext to do what you want this would be an easy way to implement it.Google textwatcher in android and try to implement it

Pramod Yadav
  • 2,316
  • 2
  • 23
  • 36
0

Try this it will help you

    YuredittextObject.addTextChangedListener(new TextWatcher(){
        public void afterTextChanged(Editable s) {

         }

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

        public void onTextChanged(CharSequence s, int start, int before, int count){
            String strEnteredVal = edittext.getText().toString();

            if(!strEnteredVal.equals("")){
            int num=Integer.parseInt(strEnteredVal);
            if(num>18&&num<85){
Toast.maketext(context,"Do not enter the values between 18 to 85",Toast.LENGTH_SHORT).show();
            }else{
             edittext.setText(""+num);             

            }
        }

    });