0

i have requirement restrict Edit Text that input type is number. i need only accept value >= 85 and value <=110 . please help. i search a lot but many people gives solution from value 1 to some limit.

  • Put TextWatcher listener on EditText and you can do two things here. Either replace the text with your desire limited range or just show the error to user – Ashwani Garg Apr 17 '20 at 20:59
  • 2
    Why not use a SeekBar instead? You can set min and max values on the SeekBar and have it update the value in a TextView to show them what they're selecting. That's much more user friendly than trying to limit a number as it's being typed. How can you stop them from typing 11 without stopping them from typing 110? – Tenfour04 Apr 17 '20 at 21:36
  • dear i have edittext that take human temperature like its allow only value between 85 to 110.. seekbar is not an option in my case. – user3514912 Apr 18 '20 at 09:29
  • Why does that prevent you from using SeekBar? Spinner might be another good alternative. – Tenfour04 Apr 18 '20 at 13:03

2 Answers2

0

Please make one class like this..

class FilterInput:InputFilter {
  private val min:Int = 0
  private val max:Int = 0
  constructor(min:Int, max:Int) {
    this.min = min
    this.max = max
  }
  constructor(min:String, max:String) {
    this.min = Integer.parseInt(min)
    this.max = Integer.parseInt(max)
  }

}

now you can use this class in your Ui(Activity/Fragment)..

etInput.filters = arrayOf<InputFilter>(FilterInput("85", "110"))
Jaimil Patel
  • 1,301
  • 6
  • 13
0

I am referring to this answer: Is there a way to define a min and max value for EditText in Android?

Best solution is, as also already answered by Jaimil, the InputFilter. But additionally to his answer, you have to override the filter() method in your custom class. Like this:

package com.test;

import android.text.InputFilter;
import android.text.Spanned;

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 {
            int input = Integer.parseInt(dest.toString() + source.toString());
            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;
    }
}

And then use it like:

EditText et = (EditText) findViewById(R.id.myEditText);
et.setFilters(new InputFilter[]{ new InputFilterMinMax("85", "1")});

E viola!

NiklasLehnfeld
  • 984
  • 6
  • 14
  • Won't this prevent you from typing anything? When you type the first digit, I would expect it to be interpreted as a one-digit number the first time `filter` is called, and it won't be bigger than 85 so it will be rejected. – Tenfour04 Apr 18 '20 at 00:17
  • As you can read here in the docs: https://developer.android.com/reference/android/text/InputFilter the filter method is not called instantly but first everything will be written in a buffer and as soon as the buffer will be cleared and the text will be processed the filter method will be called. – NiklasLehnfeld Apr 18 '20 at 07:03
  • this will not fulfill my requirement.. because it will work BTW start from 1 to unlimited... but when you set 85 is start point try to type 7 in edit text 7 is less then 85 . we are unable to type in edit input even not allow 8 ,9 etc – user3514912 Apr 18 '20 at 09:34
  • The buffer is cleared every time you type a character. The only reason there is a buffer is to handle copy-paste and auto-complete, where multiple characters can be put in the buffer at once. – Tenfour04 Apr 18 '20 at 13:00