-1

I have an editText that is set so that a user can only enter an integer value. The value is then sent to an API. The problem is that the server get's overloaded if the value is too high (Heroku problems). I was wondering if there was a way to actually limit the value that was able to be entered into the editText. For example the user can only enter a value between 1-800.

Obvioulsy I cant limit the value by number of figures, for example 3 figures, as that would allow for a value of up to 999.

There is another question open for this, however the answers in that Q dont seem to come up with one elegant solution, instead an amalgamation of different pieces of buggy code.

Here is the current MainActivity and xml for the editText.

activity_main.xml

<EditText
        android:id="@+id/editText2"
        android:layout_width="274dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="278dp"
        android:ems="10"
        android:hint="Enter number of tweets..."
        android:inputType="number" />

mainActivity.Java

EditText editText2 = findViewById(R.id.editText2);
String tweetNo = editText2.getText().toString();

With an onclicklistener for the button that draws the data from the editText as well.

JamWill
  • 134
  • 3
  • 21
  • 1
    Can you use a spinner instead of a textview, then just set the min and max values on it? – chips Mar 23 '19 at 13:49
  • This is an option if i cant find an elegant solution. I liked the edittext idea, but i will certainly go down this route if need be – JamWill Mar 23 '19 at 13:52
  • Possible duplicate of [Is there a way to define a min and max value for EditText in Android?](https://stackoverflow.com/questions/14212518/is-there-a-way-to-define-a-min-and-max-value-for-edittext-in-android) – Suraj Vaishnav Mar 23 '19 at 13:53
  • "_however the answers in that Q dont seem to come up with one elegant solution, instead an amalgamation of different pieces of buggy code_" If so,please explain why the answers given in this question doesn't work – John Joe Mar 23 '19 at 14:14
  • @JohnJoe Over half of those answers in that thread reportedly have bugs in them by the person underneath. – JamWill Mar 23 '19 at 15:16

3 Answers3

3

You just need to have this condition to check whether the value entered is between 1 to 800.

 EditText editText2 = findViewById(R.id.editText2);
 String tweetNo = editText2.getText().toString();
 int result = Integer.parseInt(tweetNo);   // convert String to int 

 if (result >= 1 && result <= 800){
      // do whatever you want
 }
John Joe
  • 12,412
  • 16
  • 70
  • 135
2

You can use an InputFilter to limit the value.

        editText2.setFilters(new InputFilter[]{new InputFilter() {
        @Override
        public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
            try{
                int val=Integer.parseInt(spanned.toString()+charSequence.toString());
                if(MIN<=val&&val<=MAX)
                    return null;
            }catch (NumberFormatException e){
            }
            return "";
        }
    }});
Sandeep Polamuri
  • 609
  • 4
  • 10
0

You can set a custom InputFilter for your EditText. InputFilter defines what value is allowed inside the EditText, in your case we need to define a filter that accepts only values within the range of 0 to 800. Define the filter class like this:

public class CustomRangeInputFilter implements InputFilter {
    private double minValue;
    private double maxValue;

    public CustomRangeInputFilter(double minVal, double maxVal) {
        this.minValue = minVal;
        this.maxValue = maxVal;
    }

    @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 newVal = dest.toString().substring(0, dStart) + dest.toString().substring(dEnd, dest.toString().length());
            newVal = newVal.substring(0, dStart) + source.toString() + newVal.substring(dStart, newVal.length());
            double input = Double.parseDouble(newVal);

            if (isInRange(minValue, maxValue, input)) {
                return null;
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return "";
    }

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

Then you set the filter on your EditText on the onCreate method:

editText.setFilters(new InputFilter[]{new CustomRangeInputFilter(0f, 800.0f)});
Shababb Karim
  • 3,614
  • 1
  • 22
  • 35